I am having a strange problem that I have not encountered before while using R, Rstudio, or stan. I am not able to get any model to compile in stan. I am pretty confident it is not the models. trivial models in brms do not compile either. Whereas rstanarm models have no problem, being pre-compiled, as I understand. Likewise, similar models compile without problems on another computer. So, something is wrong with my configurations, but that stuff is above my understanding. I have tried the old delete everything and re-install trick to no avail. I am worried that I deleted something without realizing it while cleaning up files.
I am on a PC with windows 10 pro. I have R 4.0.4. Rstudio 1.4.1103.
Everytime I try to compile a stan model I get the following output:
make cmd is
make -f "C:/PROGRA~1/R/R-40~1.4/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-40~1.4/share/make/winshlib.mk" CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB="file735032f41a17.dll" WIN=64 TCLBIN=64 OBJECTS="file735032f41a17.o"
make would use
if test "zfile735032f41a17.o" != "z"; then \
if test -e "file735032f41a17-win.def"; then \
echo "C:/rtools40/mingw64/bin/"g++ -shared -s -static-libgcc -o file735032f41a17.dll file735032f41a17-win.def file735032f41a17.o "C:/Users/loren/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a" -L"C:/Users/loren/Documents/R/win-library/4.0/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/loren/Documents/R/win-library/4.0/RcppParallel/lib/x64" -ltbb -L"C:/PROGRA~1/R/R-40~1.4/bin/x64" -lR ; \
"C:/rtools40/mingw64/bin/"g++ -shared -s -static-libgcc -o file735032f41a17.dll file735032f41a17-win.def file735032f41a17.o "C:/Users/loren/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a" -L"C:/Users/loren/Documents/R/win-library/4.0/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/loren/Documents/R/win-library/4.0/RcppParallel/lib/x64" -ltbb -L"C:/PROGRA~1/R/R-40~1.4/bin/x64" -lR ; \
else \
echo EXPORTS > tmp.def; \
"C:/rtools40/mingw64/bin/"nm file735032f41a17.o | sed -n 's/^.* [BCDRT] / /p' | sed -e '/[.]refptr[.]/d' -e '/[.]weak[.]/d' | sed 's/[^ ][^ ]*/"&"/g' >> tmp.def; \
echo "C:/rtools40/mingw64/bin/"g++ -shared -s -static-libgcc -o file735032f41a17.dll tmp.def file735032f41a17.o "C:/Users/loren/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a" -L"C:/Users/loren/Documents/R/win-library/4.0/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/loren/Documents/R/win-library/4.0/RcppParallel/lib/x64" -ltbb -L"C:/PROGRA~1/R/R-40~1.4/bin/x64" -lR ; \
"C:/rtools40/mingw64/bin/"g++ -shared -s -static-libgcc -o file735032f41a17.dll tmp.def file735032f41a17.o "C:/Users/loren/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a" -L"C:/Users/loren/Documents/R/win-library/4.0/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/loren/Documents/R/win-library/4.0/RcppParallel/lib/x64" -ltbb -L"C:/PROGRA~1/R/R-40~1.4/bin/x64" -lR ; \
rm -f tmp.def; \
fi \
fi
Error in compileCode(f, code, language = language, verbose = verbose) :
C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/loren/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a(stan_fit.o):stan_fit.cpp:(.data$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1]+0x0): multiple definition of `boost::math::detail::bessel_j0<long double>(long double)::P1'; file735032f41a17.o:file735032f41a17.cpp:(.rdata$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1]+0x0): first defined hereC:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/loren/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a(stan_fit.o):stan_fit.cpp:(.data$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2QS[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2QS]+0x0): multiple definition of `boost::math::detail::bessel_j0<long double>(long double)::QS'; file735032f41a17.o:file735032f41a
Error in sink(type = "output") : invalid connection
This is the first time I have asked a question in the forums. So, if I have not given you all the information you need, don't hesitate to ask for it. Thanks much for your help.
Seems this is common:
> sessionInfo()
R version 4.0.4 (2021-02-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rstan_2.21.2 StanHeaders_2.21.0-7 bayesplot_1.8.0 sjPlot_2.8.7 survminer_0.4.8
[6] ggpubr_0.4.0 survival_3.2-7 rstanarm_2.21.1 brms_2.14.4 Rcpp_1.0.6
[11] finalfit_1.0.2 lubridate_1.7.9.2 reshape2_1.4.4 googlesheets4_0.2.0 forcats_0.5.1
[16] stringr_1.4.0 dplyr_1.0.4 purrr_0.3.4 readr_1.4.0 tidyr_1.1.2
[21] tibble_3.0.6 ggplot2_3.3.3 tidyverse_1.3.0
loaded via a namespace (and not attached):
[1] readxl_1.3.1 backports_1.2.1 plyr_1.8.6 igraph_1.2.6 splines_4.0.4
[6] crosstalk_1.1.1 TH.data_1.0-10 rstantools_2.1.1 inline_0.3.17 digest_0.6.27
[11] htmltools_0.5.1.1 rsconnect_0.8.16 magrittr_2.0.1 openxlsx_4.2.3 modelr_0.1.8
[16] RcppParallel_5.0.2 matrixStats_0.58.0 xts_0.12.1 sandwich_3.0-0 askpass_1.1
[21] prettyunits_1.1.1 colorspace_2.0-0 rvest_0.3.6 xfun_0.20 haven_2.3.1
[26] callr_3.5.1 crayon_1.4.1 jsonlite_1.7.2 lme4_1.1-26 zoo_1.8-8
[31] glue_1.4.2 gargle_0.5.0 gtable_0.3.0 emmeans_1.5.4 sjstats_0.18.1
[36] sjmisc_2.8.6 V8_3.4.0 car_3.0-10 pkgbuild_1.2.0 abind_1.4-5
[41] scales_1.1.1 mvtnorm_1.1-1 DBI_1.1.1 ggeffects_1.0.1 rstatix_0.7.0
[46] miniUI_0.1.1.1 performance_0.7.0 xtable_1.8-4 foreign_0.8-81 km.ci_0.5-2
[51] stats4_4.0.4 DT_0.17 htmlwidgets_1.5.3 httr_1.4.2 threejs_0.3.3
[56] ellipsis_0.3.1 mice_3.13.0 pkgconfig_2.0.3 loo_2.4.1 dbplyr_2.1.0
[61] effectsize_0.4.3 tidyselect_1.1.0 rlang_0.4.10 later_1.1.0.1 munsell_0.5.0
[66] cellranger_1.1.0 tools_4.0.4 cli_2.3.0 generics_0.1.0 sjlabelled_1.1.7
[71] broom_0.7.4 ggridges_0.5.3 fastmap_1.1.0 knitr_1.31 processx_3.4.5
[76] fs_1.5.0 zip_2.1.1 survMisc_0.5.5 nlme_3.1-152 mime_0.9
[81] projpred_2.0.2 xml2_1.3.2 compiler_4.0.4 shinythemes_1.2.0 rstudioapi_0.13
[86] gamm4_0.2-6 curl_4.3 ggsignif_0.6.0 reprex_1.0.0 statmod_1.4.35
[91] stringi_1.5.3 parameters_0.11.0 ps_1.5.0 Brobdingnag_1.2-6 lattice_0.20-41
[96] Matrix_1.3-2 nloptr_1.2.2.2 markdown_1.1 KMsurv_0.1-5 shinyjs_2.0.0
[101] vctrs_0.3.6 pillar_1.4.7 lifecycle_1.0.0 bridgesampling_1.0-0 estimability_1.3
[106] insight_0.13.0 data.table_1.13.6 httpuv_1.5.5 R6_2.5.0 promises_1.1.1
[111] gridExtra_2.3 rio_0.5.16 codetools_0.2-18 boot_1.3-26 colourpicker_1.1.0
[116] MASS_7.3-53 gtools_3.8.2 assertthat_0.2.1 openssl_1.4.3 withr_2.4.1
[121] shinystan_2.5.0 multcomp_1.4-16 bayestestR_0.8.2 mgcv_1.8-33 parallel_4.0.4
[126] hms_1.0.0 grid_4.0.4 coda_0.19-4 minqa_1.2.4 carData_3.0-4
[131] googledrive_1.0.1 shiny_1.6.0 base64enc_0.1-3 dygraphs_1.1.1.6
The following very simple stan model elicits the above error
model <- "
data{
int<lower=0> N;
vector[N] Y;
vector[N] X;
}
parameters{
real beta;
real mu;
real<lower=0> sigma;
}
model{
Y ~ normal(mu + beta*X, sigma);
mu ~ normal(0, 1);
beta ~ normal(0, 1);
sigma ~ gamma(0.1, 0.1);
}"
stan_model(model_code = model)
I was able to stumble onto a solution. I re-installed rstan. I have no idea what the problem was or why this fixed it. If others would like to weigh in, that would be much appreciated. Thanks to those who put any thought into my query.
Related
I recently updated my Rstudio to V2022.07.2+576, but found Rcpp can no longer compile code.
library(Rcpp)
cppFunction('int add(int x, int y, int z) {
int sum = x + y + z;
return sum;
}')
, which give me error message like:
clang-12: warning: include path for libstdc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found]
In file included from file7898532e8a3d.cpp:1:
In file included from /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/Rcpp/include/Rcpp.h:27:
In file included from /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/Rcpp/include/RcppCommon.h:30:
In file included from /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/Rcpp/include/Rcpp/r/headers.h:62:
/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/Rcpp/include/Rcpp/platform/compiler.h:100:10: fatal error: 'cmath' file not found
#include <cmath>
^~~~~~~
1 error generated.
make: *** [/Library/Frameworks/R.framework/Resources/etc/Makeconf:176: file7898532e8a3d.o] Error 1
clang++ -arch arm64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/Rcpp/include" -I"/private/var/folders/0l/_zb89yxd4qs6g7t3dx2t__k80000gr/T/RtmpPHmvlY/sourceCpp-aarch64-apple-darwin20-1.0.9" -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -c file7898532e8a3d.cpp -o file7898532e8a3d.o
Error in sourceCpp(code = code, env = env, rebuild = rebuild, cacheDir = cacheDir, :
Error 1 occurred building shared library.
I tried to reinstall R/Rstudio, and update Xcode, but had no good luck. Here is my sessioninfo
R version 4.2.2 (2022-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Big Sur 11.4
Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] Rcpp_1.0.9 MASS_7.3-58.1 doParallel_1.0.17 iterators_1.0.14 foreach_1.5.2
loaded via a namespace (and not attached):
[1] xfun_0.31 tidyselect_1.1.2 purrr_0.3.4 splines_4.2.2 operator.tools_1.6.3
[6] lattice_0.20-45 vctrs_0.4.1 generics_0.1.3 htmltools_0.5.3 GWASExactHW_1.01
[11] stats4_4.2.2 yaml_2.3.5 mgcv_1.8-41 utf8_1.2.2 rlang_1.0.4
[16] pillar_1.8.0 glue_1.6.2 withr_2.5.0 DBI_1.1.3 BiocGenerics_0.42.0
[21] gdsfmt_1.32.0 GenomeInfoDbData_1.2.8 lifecycle_1.0.1 zlibbioc_1.42.0 Biostrings_2.64.0
[26] codetools_0.2-18 evaluate_0.15 Biobase_2.56.0 knitr_1.39 SeqArray_1.36.2
[31] IRanges_2.30.0 fastmap_1.1.0 GenomeInfoDb_1.32.2 fansi_1.0.3 broom_1.0.0
[36] backports_1.4.1 S4Vectors_0.34.0 XVector_0.36.0 digest_0.6.29 formula.tools_1.7.1
[41] dplyr_1.0.9 GenomicRanges_1.48.0 grid_4.2.2 cli_3.3.0 tools_4.2.2
[46] bitops_1.0-7 magrittr_2.0.3 RCurl_1.98-1.7 tibble_3.1.8 mice_3.14.0
[51] crayon_1.5.1 tidyr_1.2.0 pkgconfig_2.0.3 Matrix_1.5-1 data.table_1.14.2
[56] assertthat_0.2.1 rmarkdown_2.14 rstudioapi_0.13 logistf_1.24.1 R6_2.5.1
[61] SeqVarTools_1.34.0 nlme_3.1-160 compiler_4.2.2
This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 1 year ago.
When following their documentation and running ./build_packages --board=lakitu, I get the following error.
Using ubuntu 16.0.4. Looks like a sed syntax error? Am I missing a variable? Does sed work differently in different operating systems or is something wrong with their documentation/scripts? Followed their documentation to the t and didn't add or configure anything. Waiting for a successful run first.
Looking at similar questions, they all appear to be syntax errors...
* Package: sys-boot/shim-14.0.20180308-r4
* Repository: lakitu
* USE: abi_x86_64 amd64 elibc_glibc kernel_linux userland_GNU
* FEATURES: network-sandbox sandbox splitdebug userpriv usersandbox
* Running stacked hooks for pre_pkg_setup
* sysroot_build_bin_dir ... [ ok ]
* Running stacked hooks for post_pkg_setup
* python_eclass_hack ... [ ok ]
* Running stacked hooks for pre_src_unpack
* python_multilib_setup ... [ ok ]
>>> Unpacking source...
>>> Unpacking shim-14.0.20180308.tar.gz to /build/lakitu/tmp/portage/sys-boot/shim-14.0.20180308-r4/work
>>> Source unpacked in /build/lakitu/tmp/portage/sys-boot/shim-14.0.20180308-r4/work
* Running stacked hooks for post_src_unpack
* asan_init ... [ ok ]
>>> Preparing source in /build/lakitu/tmp/portage/sys-boot/shim-14.0.20180308-r4/work/shim-79cdb2a215de2ace7d1bf0a294165a04b726c70a ...
>>> Source prepared.
>>> Configuring source in /build/lakitu/tmp/portage/sys-boot/shim-14.0.20180308-r4/work/shim-79cdb2a215de2ace7d1bf0a294165a04b726c70a ...
>>> Source configured.
>>> Compiling source in /build/lakitu/tmp/portage/sys-boot/shim-14.0.20180308-r4/work/shim-79cdb2a215de2ace7d1bf0a294165a04b726c70a ...
make -j8 ARCH=x86_64 CROSS_COMPILE=x86_64-cros-linux-gnu- EFI_INCLUDE=/build/lakitu//usr/include/efi EFI_PATH=/build/lakitu//usr/lib64 ARCH_LDFLAGS=--no-experimental-use-relr COMMITID=79cdb2a215de2ace7d1bf0a294165a04b726c70a DEFAULT_LOADER=\\\\grub-lakitu.efi shimx64.efi
sed -e "s,##VERSION##,14," \
-e "s,##UNAME##,Linux x86_64 Intel Xeon E312xx (Sandy Bridge, IBRS update) GenuineIntel GNU/Linux," \
-e "s,##COMMIT##,79cdb2a215de2ace7d1bf0a294165a04b726c70a," \
< /build/lakitu/tmp/portage/sys-boot/shim-14.0.20180308-r4/work/shim-79cdb2a215de2ace7d1bf0a294165a04b726c70a/version.c.in > version.c
sed: -e expression #2, char 60: unknown option to `s'
make: *** [Makefile:183: version.c] Error 1
* ERROR: sys-boot/shim-14.0.20180308-r4::lakitu failed (compile phase):
* emake failed
*
* If you need support, post the output of `emerge --info '=sys-boot/shim-14.0.20180308-r4::lakitu'`,
* the complete build log and the output of `emerge -pqv '=sys-boot/shim-14.0.20180308-r4::lakitu'`.
* The complete build log is located at '/build/lakitu/tmp/portage/logs/sys-boot:shim-14.0.20180308-r4:20190531-002217.log'.
* For convenience, a symlink to the build log is located at '/build/lakitu/tmp/portage/sys-boot/shim-14.0.20180308-r4/temp/build.log'.
* The ebuild environment file is located at '/build/lakitu/tmp/portage/sys-boot/shim-14.0.20180308-r4/temp/environment'.
* Working directory: '/build/lakitu/tmp/portage/sys-boot/shim-14.0.20180308-r4/work/shim-79cdb2a215de2ace7d1bf0a294165a04b726c70a'
* S: '/build/lakitu/tmp/portage/sys-boot/shim-14.0.20180308-r4/work/shim-79cdb2a215de2ace7d1bf0a294165a04b726c70a'
There's a , after Bridge
-e "s,##UNAME##,Linux x86_64 Intel Xeon E312xx (Sandy Bridge, IBRS update) GenuineIntel GNU/Linux," \
Change to
-e "s###UNAME###Linux x86_64 Intel Xeon E312xx (Sandy Bridge, IBRS update) GenuineIntel GNU/Linux#" \
I've built a for loop to generate a series of PDF files automatically, but it says:
output file: test_chinese.knit.md
"C:/PROGRA~2/Pandoc/pandoc" +RTS -K512m -RTS test_chinese.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output 1TEST.tex --template "C:\Users\adminstor\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --pdf-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in" --variable "compact-title:yes"
This is pdfTeX, Version 3.14159265-2.6-1.40.19 (MiKTeX 2.9.6650 64-bit)
entering extended mode
! Argument of \CJK#XX has an extra }.
<inserted text>
\par
l.30 breaklinks=true}
Error: Failed to compile 1TEST.tex. See 1TEST.log for more info.
In addition: Warning messages:
1: running command '"pdflatex" -halt-on-error -interaction=batchmode "1TEST.tex"' had status 1
2: running command '"pdflatex" -halt-on-error -interaction=batchmode "1TEST.tex"' had status 1
Have to mention that: The .rmd file could run successfully without the for loop.
The for loop I built is:
for (i in 1:5){
cat(i, '\n')
rmarkdown::render("test_chinese.Rmd",
encoding = 'UTF-8',
params = list(file_number = i),
output_file = paste0(i, "TEST.pdf"),
output_format = 'pdf_document')
}
Since I'm generating a Chinese file, the TeX Template is CTeX Template with TinyTex.
Here is my sessionInfo():
> sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936 LC_CTYPE=Chinese (Simplified)_China.936 LC_MONETARY=Chinese (Simplified)_China.936
[4] LC_NUMERIC=C LC_TIME=Chinese (Simplified)_China.936
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rvest_0.3.2 xml2_1.2.0 knitr_1.20 rmarkdown_1.10
loaded via a namespace (and not attached):
[1] Rcpp_0.12.16 magrittr_1.5 devtools_1.13.6 R6_2.2.2 stringr_1.3.1 httr_1.3.1 highr_0.6 tools_3.4.4
[9] xfun_0.3 tinytex_0.8 postr_0.0.0 git2r_0.23.0 withr_2.1.2 selectr_0.4-1 htmltools_0.3.6 yaml_2.2.0
[17] rprojroot_1.3-2 digest_0.6.15 rsconnect_0.8.8 curl_3.2 memoise_1.1.0 evaluate_0.11 stringi_1.1.7 compiler_3.4.4
[25] rticles_0.5 backports_1.1.2
Looking forward for your help!
Thanks everyone.
Is there any way to get back the mangled name from demangled name in g++.
For example , I have the demangled name func(char*, int), what should I do to get the mangled name i.e _Z4funcPci back?
My question is g++ specific.
You can simply use g++ to compile an empty function with the signature you require and extract the name from that. For example:
echo "int f1(char *, int) {} " | g++ -x c++ -S - -o- | grep "^_.*:$" | sed -e 's/:$//'
gives output
_Z2f1Pci
which is I think what you require. Make sure that you include any relevant header files as they will affect the way the symbols are mangled.
Based on the Bojan Nikolic's approach, here's a better script:
mangle.bash:
IFS='::' read -a array <<< "$1"
indexes=("${!array[#]}")
prefix=""
middle=""
suffix=""
rettype=""
if [ -z "$2" ]; then
rettype="void"
fi
for index in "${indexes[#]}"
do
#echo "$index ${array[index]}"
if [ $index == ${indexes[-1]} ]; then
#echo "last"
middle="$rettype ${array[index]};"
elif [ -n "${array[index]}" ]; then
#echo "not empty"
prefix="${prefix}struct ${array[index]}{"
suffix="${suffix}};"
fi
done
#echo "$prefix$middle$suffix $rettype $1{}"
echo "$prefix$middle$suffix $rettype $1{}" | g++ -x c++ -S - -o- | grep "^_.*:$" | sed -e 's/:$//'
Use:
$ ./mangle.bash "abc::def::ghi()"
_ZN3abc3def3ghiEv
$ ./mangle.bash "abc::def::ghi(int i, char c)"
_ZN3abc3def3ghiEic
$ ./mangle.bash "abc::def::def(int i, char c)" constr
_ZN3abc3defC2Eic
$ ./mangle.bash "abc::def::~def()" destr
_ZN3abc3defD2Ev
But as to constructors and destructors, remember that there are C0 C1 C2 and D0 D1 D2 ones.
What's worst, sometimes you cannot mangle a name because you must get more than one result.
See https://reverseengineering.stackexchange.com/q/4323/4398 (there are multiple destructors in VFT, and all of them are demangled as ClassName::~ClassName()). (The same applies to constructors, I have seen C0 and C2 constructors.)
On the other hand, that answer references the Itanium ABI: https://refspecs.linuxbase.org/cxxabi-1.75.html#mangling-type where mangling is specified.
The itanium-abi Haskell package: it did not work for me (May 2014)
There is a Haskell package
http://hackage.haskell.org/package/itanium-abi
that promises both demangling and mangling, but I could run only the demangling:
Installation on Ubuntu Precise:
sudo aptitude install ghc
sudo aptitude install cabal-install
cabal update
cabal install itanium-abi
Then you run ghci and after import ABI.Itanium and import Data.Either you get:
Prelude ABI.Itanium Data.Either> cxxNameToText $ head (rights [ demangleName "_ZTI13QSystemLocale" ])
"typeinfo for QSystemLocale"
There is mangleName, but it takes a DecodedName which is a data structure rather than a string, and that data structure is produced only by demangleName (unless I overlooked something). Hopefully, this will get better in some future release.
The clang code
I did not try the clang code.
Is there any way to get back the mangled name from demangled name in g++.
For example , I have the demangled name func(char*, int), what should I do to get the mangled name i.e _Z4funcPci back?
My question is g++ specific.
You can simply use g++ to compile an empty function with the signature you require and extract the name from that. For example:
echo "int f1(char *, int) {} " | g++ -x c++ -S - -o- | grep "^_.*:$" | sed -e 's/:$//'
gives output
_Z2f1Pci
which is I think what you require. Make sure that you include any relevant header files as they will affect the way the symbols are mangled.
Based on the Bojan Nikolic's approach, here's a better script:
mangle.bash:
IFS='::' read -a array <<< "$1"
indexes=("${!array[#]}")
prefix=""
middle=""
suffix=""
rettype=""
if [ -z "$2" ]; then
rettype="void"
fi
for index in "${indexes[#]}"
do
#echo "$index ${array[index]}"
if [ $index == ${indexes[-1]} ]; then
#echo "last"
middle="$rettype ${array[index]};"
elif [ -n "${array[index]}" ]; then
#echo "not empty"
prefix="${prefix}struct ${array[index]}{"
suffix="${suffix}};"
fi
done
#echo "$prefix$middle$suffix $rettype $1{}"
echo "$prefix$middle$suffix $rettype $1{}" | g++ -x c++ -S - -o- | grep "^_.*:$" | sed -e 's/:$//'
Use:
$ ./mangle.bash "abc::def::ghi()"
_ZN3abc3def3ghiEv
$ ./mangle.bash "abc::def::ghi(int i, char c)"
_ZN3abc3def3ghiEic
$ ./mangle.bash "abc::def::def(int i, char c)" constr
_ZN3abc3defC2Eic
$ ./mangle.bash "abc::def::~def()" destr
_ZN3abc3defD2Ev
But as to constructors and destructors, remember that there are C0 C1 C2 and D0 D1 D2 ones.
What's worst, sometimes you cannot mangle a name because you must get more than one result.
See https://reverseengineering.stackexchange.com/q/4323/4398 (there are multiple destructors in VFT, and all of them are demangled as ClassName::~ClassName()). (The same applies to constructors, I have seen C0 and C2 constructors.)
On the other hand, that answer references the Itanium ABI: https://refspecs.linuxbase.org/cxxabi-1.75.html#mangling-type where mangling is specified.
The itanium-abi Haskell package: it did not work for me (May 2014)
There is a Haskell package
http://hackage.haskell.org/package/itanium-abi
that promises both demangling and mangling, but I could run only the demangling:
Installation on Ubuntu Precise:
sudo aptitude install ghc
sudo aptitude install cabal-install
cabal update
cabal install itanium-abi
Then you run ghci and after import ABI.Itanium and import Data.Either you get:
Prelude ABI.Itanium Data.Either> cxxNameToText $ head (rights [ demangleName "_ZTI13QSystemLocale" ])
"typeinfo for QSystemLocale"
There is mangleName, but it takes a DecodedName which is a data structure rather than a string, and that data structure is produced only by demangleName (unless I overlooked something). Hopefully, this will get better in some future release.
The clang code
I did not try the clang code.