[PPL-devel] GMP memory allocation problem in SWI-Prolog 5.6.38 and following versions

Pedro Baltazar Vasconcelos pbv at dcc.fc.up.pt
Mon Oct 8 10:59:00 CEST 2007


On Wed, 03 Oct 2007 14:20:21 +0200
Roberto Bagnara <bagnara at cs.unipr.it> wrote:


>  > I used objcopy on the library binaries rather than modify the
>  > sources. But for some reason I could only get this to work with the
>  > static libraries. Anyway, I can pass you a short script that does
>  > the library renaming if you are interested.
> 
> Yes, please!  Why not posting it here?


OK, here is my library renaming script. Please read the comments for instructions and feel free to contact me if you have any questions or sugestions.

Best regards,

Pedro

#!/usr/bin/tclsh
#
# This script renames symbols in the static GMP and PPL libraries
# this avoid issues with FFI linking to other systems that 
# modify the GMP allocator e.g. the Glasgow Haskell compiler.
# Tested under Unbuntu Linux 6.06.
#
# Requires libgmp.a, libgmpxx.a, libppl.a, libppl_c.a (location below)
# Creates copies libmy*.a with renamed GMP symbols in current directory
# Does *not* modify the original libraries.
#
# To run: "tclsh librename.tcl"
# 
# To install the libraries: manually copy libmy*.a to a suitable
# system wide directory (e.g. /usr/local/lib).
# 
# You should then link the foreign code with libmy*.a;
# the external PPL interface is the same, but libmygmp is private.
#
# Pedro Vasconcelos, 2004, 2007

# location of the GMP and PPL *static* libraries
# modify this according to your installation
set PPL_DIR /usr/local/lib
set GMP_DIR /usr/lib

# location for temporary files
set TMP_DIR /tmp

# auxiliary procedure to create a renaming map 
# of all defined symbols in a library 
# $obj    : the library archive file
# $prefix : a string prepended to each symbol 
# $out    : the output text file with the renaming map
proc scan_object {obj prefix out} {
    set fd [open $out w]
    # execute nm to extract the symbols
    foreach line [split [exec nm -A --defined-only $obj 2> /dev/null] \n] {
	# process each line
	set line [split $line]
	set type [lindex $line 1]
	set symbol [lindex $line 2]
	if {[string first $type "BDTR"] >= 0} {
	    puts $fd ${symbol}\t${prefix}${symbol}
	}
    }
    close $fd
}

# create maps for C and C++ GMP libraries
puts "Scanning libraries..."
scan_object ${GMP_DIR}/libgmp.a   "my" ${TMP_DIR}/libgmp.ren
scan_object ${GMP_DIR}/libgmpxx.a "my" ${TMP_DIR}/libgmpxx.ren

# combine the renaming maps into a unique one
exec cat libgmp.ren libgmpxx.ren | sort | uniq > ${TMP_DIR}/libgmpall.ren

# create renamed versions of the libraries 
# (in the current working directory)
puts "Creating renamed libraries..."
exec objcopy --redefine-syms=${TMP_DIR}/libgmp.ren \
    ${GMP_DIR}/libgmp.a libmygmp.a
exec objcopy --redefine-syms=${TMP_DIR}/libgmpxx.ren \
    ${GMP_DIR}/libgmpxx.a libmygmpxx.a

exec objcopy --redefine-syms=${TMP_DIR}/libgmpall.ren \
    ${PPL_DIR}/libppl.a libmyppl.a
exec objcopy --redefine-syms=${TMP_DIR}/libgmpall.ren \
    ${PPL_DIR}/libppl_c.a libmyppl_c.a

# remove temporary files
file delete ${TMP_DIR}/libgmp.ren ${TMP_DIR}/libgmpxx.ren ${TMP_DIR}/libgmpall.ren

puts "Done!"

# -- end of file --



More information about the PPL-devel mailing list