Re: static linking

David Daney <[email protected]>
Newsgroups gmane.comp.gcc.java.devel
Message-ID <[email protected]>
Daniel Andrzejewski wrote:
> Hi all,
> 
> I have a problem with gcj. I have read couple threads here, but I still 
> have no idea how to fix my problem. Usually people say that this is hard 
> but doable, but they don't give much directions.
> 
> I'm trying to do it for a system where there's no shared libraries, so I 
> must not even dynamically link libc.

One might ask the question: Why?

> 
> I successfully build gcc 4.3.0 with --disable-shared option. I can 
> successfully compile a java program, but the executable is still 
> dynamically linked.
> 
> /scratch/daniel> ldd ./HelloWorld
>         libz.so.1 => /lib64/libz.so.1 (0x00002adcd28dd000)
>         libpthread.so.0 => /lib64/tls/libpthread.so.0 (0x00002adcd29f0000)
>         libc.so.6 => /lib64/tls/libc.so.6 (0x00002adcd2b04000)
>         libm.so.6 => /lib64/tls/libm.so.6 (0x00002adcd2d26000)
>         libdl.so.2 => /lib64/libdl.so.2 (0x00002adcd2e7d000)
>         librt.so.1 => /lib64/tls/librt.so.1 (0x00002adcd2f80000)
>         /lib64/ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2 
> (0x00002adcd27c6000)
> 
> When I use -static flag while compiling I get some warnings, but the 
> executable gets compiled.
> 
> /scratch/daniel> gcj --main=HelloWorld -o HelloWorld -static 
> HelloWorld.java
> ...
> warnings
> ...

Do you know a priori that the content of the warnings is not relevant to this problem?  If not, please include them.

First start with only -static-libgcj to isolate C library problems from libgcj problems.


> /scratch/daniel> file HelloWorld
> HelloWorld: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for 
> GNU/Linux 2.4.1, statically linked, not stripped
> 
> 
> And the problem is at the runtime:
> 
> /scratch/daniel> ./HelloWorld
> Aborted
> 

That is not too helpful.  You need to run the thing under gdb and at a minimum see where it is failing (put a breakpoint on abort()). 


> 
> Has anyone solved this issue yet?
> 

I have not used a static glibc, but I successfully do static linking of libgcj.  Most problems come down to missing classes due to the linker not linking classes that are only referred to dynamically.

I put the following into my main code to force some necessary classes to be linked:

extern int _ZN3gnu4java6locale8Calendar6class$E;
int *dummy1 = &_ZN3gnu4java6locale8Calendar6class$E;

extern int _ZN3gnu4java6locale17LocaleInformation6class$E;
int *dummy2 = &_ZN3gnu4java6locale17LocaleInformation6class$E;

extern int _ZN3gnu3gcj7convert12Output_ASCII6class$E;
int *dummy3 = &_ZN3gnu3gcj7convert12Output_ASCII6class$E;

extern int _ZN3gnu3gcj7convert11Output_UTF86class$E;
int *dummy5 = &_ZN3gnu3gcj7convert11Output_UTF86class$E;

#ifdef STATIC_LIBGCJ_4_3
extern int _ZGr32_java$Sutil$Siso4217$_properties;
int *dummy6 = &_ZGr32_java$Sutil$Siso4217$_properties;

extern int _ZN3gnu3xml8aelfred29XmlParser6class$E;
int *dummy7 = &_ZN3gnu3xml8aelfred29XmlParser6class$E;

#endif


Note that either fewer or more classes may be needed by your application.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.