Skip to content

Instantly share code, notes, and snippets.

@GunnarFarneback
Created June 28, 2012 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GunnarFarneback/3013408 to your computer and use it in GitHub Desktop.
Save GunnarFarneback/3013408 to your computer and use it in GitHub Desktop.
Symbol collision in Julia
gcc -Wall -fPIC -O3 -c test_move.c
gcc -shared -Wl,-soname,libtest_move.so -o libtest_move.so test_move.o
gcc -o test_move test_move.c
#include <stdio.h>
int foo;
int move[10000];
void test_move()
{
printf("foo: %p\nmove: %p\n", &foo, move);
move[9999] = 13;
}
int main(int argc, char **argv)
{
test_move();
return 0;
}
lib_test_move = dlopen("./libtest_move.so")
test_move = dlsym(lib_test_move, :test_move)
println("foo: ", dlsym(lib_test_move, :foo))
println("move: ", dlsym(lib_test_move, :move))
ccall(test_move, Void, ())
main_program = ccall(:jl_load_dynamic_library, Ptr{Void}, (Ptr{Uint8},), 0)
lib_test_move = dlopen("./libtest_move.so")
test_move = dlsym(lib_test_move, :test_move)
println("foo: ", dlsym(lib_test_move, :foo))
println("move: ", dlsym(lib_test_move, :move))
println("main_program move: ", dlsym(main_program, :move))
ccall(test_move, Void, ())
libdl = dlopen("/usr/lib/x86_64-linux-gnu/libdl.so")
function dlopen2(lib)
# From bits/dlfcn.h
const RTLD_LAZY = 0x00001
const RTLD_DEEPBIND = 0x00008
flags = RTLD_LAZY | RTLD_DEEPBIND
ccall(dlsym(libdl, :dlopen), Ptr{Void}, (Ptr{Uint8}, Int32), lib, flags)
end
function dlsym2(lib, sym)
ccall(dlsym(libdl, :dlsym), Ptr{Void}, (Ptr{Void}, Ptr{Uint8}), lib, sym)
end
lib_test_move = dlopen2("./libtest_move.so")
test_move = dlsym2(lib_test_move, :test_move)
println("foo: ", dlsym2(lib_test_move, :foo))
println("move: ", dlsym2(lib_test_move, :move))
ccall(test_move, Void, ())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment