This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Library problem in jni.cc on Linux


On Thu, May 03, 2001 at 03:04:29PM -0600, Tom Tromey wrote:
> >>>>> "Martin" == Martin Kahlert <martin.kahlert@infineon.com> writes:
> 
> Martin> The real problem seems to be the final line
> Martin> return lt_dlsym (NULL, symname);
> Martin> inside java/lang/natRuntime.cc:74
> 
> This is a thinko on my part.  Try this patch (which I haven't even
> tried to compile...).
> 
> FYI: the stuff you are doing ought to work.  However it has obviously
> not been used.  So I'm afraid you are shaking out all the latent
> bugs...

Thanks Tom,
your bugfix helped, just not at once ;-)

The problem is, that _Jv_FindSymbolInExecutable is called for the
native routines inside the class, which contains my main() function
*before* java::lang::Runtime::init has been called. 
Thus your changes do not have any effect on this.

I changed JvRunMain to call java::lang::Runtime::getRuntime() before
we start the main thread.

I do not know, why this triggers the initialization of static class members.
I assumed, they would be initialized at program start somehow automagically.

If i do not do that, the last instruction of JvRunMain calls
java::lang::Runtime::getRuntime ()->_exit(status);
which triggers java::lang::Runtime::init() to be called.
Unfortunately it is too late, then.

Another fix, that worked was to call the initialization of lt_dlinit together
with your fix if we get into _Jv_FindSymbolInExecutable and did not call the
initialization up to now.

I feel, that the change in prims.cc is more elegant and use that for now.

With these changes i get a statically linked simple prog working.

Here is the complete patch:


2001-05-04  Martin Kahlert  <martin.kahlert@infineon.com>

	* prims.cc (JvRunMain): Make sure, that runtime has been
	initialized before main_thread is started

2001-05-03  Tom Tromey  <tromey@redhat.com>

	* java/lang/natRuntime.cc (init): Call add_library on the program
	itself.
	(_Jv_FindSymbolInExecutable): Return NULL if no library on the
	list has the symbol.

diff -rc gcc-20010430.orig/libjava/java/lang/natRuntime.cc gcc-20010430/libjava/java/lang/natRuntime.cc
*** gcc-20010430.orig/libjava/java/lang/natRuntime.cc	Mon Mar 12 08:33:57 2001
--- gcc-20010430/libjava/java/lang/natRuntime.cc	Fri May  4 11:37:07 2001
***************
*** 69,75 ****
  	return r;
      }
  
!   return lt_dlsym (NULL, symname);
  }
  
  #endif /* USE_LTDL */
--- 69,75 ----
  	return r;
      }
  
!   return NULL;
  }
  
  #endif /* USE_LTDL */
***************
*** 191,196 ****
--- 191,199 ----
    finalize_on_exit = false;
  #ifdef USE_LTDL
    lt_dlinit ();
+   lt_dlhandle self = lt_dlopen (NULL);
+   if (self != NULL)
+     add_library (self);
  #endif
  }
  
diff -rc gcc-20010430.orig/libjava/prims.cc gcc-20010430/libjava/prims.cc
*** gcc-20010430.orig/libjava/prims.cc	Fri Mar 23 20:16:40 2001
--- gcc-20010430/libjava/prims.cc	Fri May  4 11:48:39 2001
***************
*** 849,860 ****
    arg_vec = JvConvertArgv (argc - 1, argv + 1);
    main_thread = new gnu::gcj::runtime::FirstThread (klass, arg_vec);
  
    main_thread->start();
    _Jv_ThreadWait ();
  
    int status = (int) java::lang::ThreadGroup::had_uncaught_exception;
      
!   java::lang::Runtime::getRuntime ()->_exit (status);
  }
  
  void
--- 849,866 ----
    arg_vec = JvConvertArgv (argc - 1, argv + 1);
    main_thread = new gnu::gcj::runtime::FirstThread (klass, arg_vec);
  
+   /* Initialize Runtime before we start the main_thread.
+    * This ensures, that the libraries are initialized even
+    * if the main class contains native JNI methods, which were 
+    * statically linked into the executable */
+   java::lang::Runtime *rt = java::lang::Runtime::getRuntime ();
+ 
    main_thread->start();
    _Jv_ThreadWait ();
  
    int status = (int) java::lang::ThreadGroup::had_uncaught_exception;
      
!   rt->_exit (status);
  }
  
  void

-- 
The early bird gets the worm. If you want something else for       
breakfast, get up later.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]