LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Segmentation fault after second dlopen() attempt (https://www.linuxquestions.org/questions/programming-9/segmentation-fault-after-second-dlopen-attempt-787636/)

NTxC 02-07-2010 04:20 PM

Segmentation fault after second dlopen() attempt
 
Hello!
My name's Patrick, and I'm a mod developer for a game named Jedi Knight 2: Jedi Outcast.

The game is quite old (2002!) and I'm trying to mod the old version of it (1.02a), the jk2ded server linux binary.

The game engine loads my mod's .so file which I compiled using:
Code:

gcc -shared -static -g -fPIC g_syscalls.c common.c main.c -o out/jk2mpgamei386.so
So whilst launching the server for the first time, it loads the .so file using dlopen() without problems and the output is:
Code:

Loading dll file jk2mpgame.
Sys_LoadDll(/web/web11/jk2/base/jk2mpgamei386.so)...
Sys_LoadDll(jk2mpgame) found **vmMain** at  0xb2ed9413 
Sys_LoadDll(jk2mpgame) succeeded!
[ NT's Fix ] GAME_INIT

and then it works correctly until I try to change the map the server is on currently.
When a map changes on the server, the .so file has to be unloaded.
After unloading with dlclose() without errors, when the new map is launched the engine tries to reload the .so into the memory with dlopen() but fails:
Code:

map_restart 0
Cvar_Set2: sv_serverid 78710500
==== ShutdownGame ====
ShutdownGame:
------------------------------------------------------------
[ NT's Fix ] GAME_SHUTDOWN
Loading dll file jk2mpgame.
Sys_LoadDll(/web/web11/jk2/base/jk2mpgamei386.so)...
Speicherzugriffsfehler

...crash with a segmentation fault error.

So I used gdb to find out why it does that, but all I got is:
Code:

(gdb) bt
#0  0x081099f0 in _dl_lookup_versioned_symbol (undef_name=0xb2efd81f "strlen", undef_map=0x838fee8, ref=0xbfdd56b8, symbol_scope=0x8390084, version=0x83901a0, type_class=0,
    explicit=0) at do-lookup.h:52
#1  0x0812248b in _dl_relocate_object () at ../sysdeps/i386/dl-machine.h:348
#2  0x0810b987 in dl_open_worker (a=0xbfdd592c) at dl-open.c:294
#3  0x0810ae67 in _dl_catch_error (objname=0xbfdd5924, errstring=0xbfdd5928, operate=0x810b570 <dl_open_worker>, args=0xbfdd592c) at dl-error.c:152
#4  0x0810bac3 in _dl_open (file=0x8242ee0 "/web/web11/jk2/base/jk2mpgamei386.so", mode=-2147483646, caller=0x80b6778) at dl-open.c:407
#5  0x080dbeef in dlopen_doit (a=0xbfdd5a8c) at dlopen.c:39
#6  0x0810ae67 in _dl_catch_error (objname=0x829a3a0, errstring=0x829a3a4, operate=0x80dbed0 <dlopen_doit>, args=0xbfdd5a8c) at dl-error.c:152
#7  0x080dc132 in _dlerror_run (operate=0x80dbed0 <dlopen_doit>, args=0xbfdd5a8c) at dlerror.c:130
#8  0x080dbebd in __dlopen_check (file=0x8242ee0 "/web/web11/jk2/base/jk2mpgamei386.so", mode=2) at dlopen.c:53
#9  0x080b6778 in .B18.8 ()
#10 0x08242ee0 in fs_gamedir ()
(gdb)

Could you please tell me what could be the possible reason of why it crashes?

Kind regards
Patrick :-)

ta0kira 02-07-2010 10:16 PM

Can you make it unload/reload without changing maps? If so, try it out. That should tell you if it's a problem with the map change. Does the mod get uploaded to another machine, i.e. does it run from a different machine than the one it's compiled on? If not, try dropping -static, also.
Kevin Barry

PS Do nm out/jk2mpgamei386.so | grep ' U '. If you see anything, that's a problem (in this particular case; it's normal otherwise.)

NTxC 02-07-2010 10:32 PM

Quote:

Originally Posted by ta0kira (Post 3856233)
Can you make it unload/reload without changing maps? If so, try it out. That should tell you if it's a problem with the map change. Does the mod get uploaded to another machine, i.e. does it run from a different machine than the one it's compiled on? If not, try dropping -static, also.
Kevin Barry

PS Do nm out/jk2mpgamei386.so | grep ' U '. If you see anything, that's a problem (in this particular case; it's normal otherwise.)

Hello Kevin,
Thanks for the reply. I'm not able to unload the .so without map change. I am compiling the jk2mpgamei386.so in Cygwin under Windows 7 and I run it at a different machine (Linux server - Debian Etch 2.6.24).

I did the command.
Here are the results:
Code:

patrickw@ulm192:~/jk2$ nm base/jk2mpgamei386.so | grep ' U '
        U strcmp@@GLIBC_2.0
        U strlen@@GLIBC_2.0
        U strstr@@GLIBC_2.0
        U vsnprintf@@GLIBC_2.0

I assume that means there are undefined symbols and it's a problem as you said, so could you give me clues on how to fix this?

I noticed one of the undefined symbols (strlen) matches frame from the gdb backtrace.

Code:

#0  0x081099f0 in _dl_lookup_versioned_symbol (undef_name=0xb2efd81f "strlen"
Could this possibly be the problem?

Thank you for the reply again,
kind regards :-)

Patrick

NTxC 02-08-2010 09:49 AM

Hello again.
I removed all references to strlen and the rest of the undefined symbols from my code so that the nm command I used doesn't print anything anymore.

Now it crashes with a reference to __deregister_frame_info_bases which is in libc.so.6.

I think I should link the libc with my .so statically. How can I achieve this?

I have the libc.a file that I want to link to my .so file but I don't know how to launch gcc so it compiles with libc statically.

Can you help me out?

Cheers,
Patrick


_______________


EDIT:

I managed to solve my problem.
I used this command to compile statically linked jk2mpgamei386.so:
Code:

gcc-linux -shared -static -static-libgcc -L. -lc -Bstatic g_syscalls.c common.c main.c -o out/jk2mpgamei386.so libc.a
This way it works without problems.

Thanks,
Patrick


All times are GMT -5. The time now is 12:51 AM.