Search code examples
javacjava-native-interfacenativesolaris

How to solve "fatal: relocations remain against allocatable but non-writable sections" while using Java native interface?


I'm trying to call a C function inside a Java code. I have this hava code.

public class JavaToC {

    public native void helloC();

    static {
        System.loadLibrary("HelloWorld");
    }

    public static void main(String[] args) {
        new JavaToC().helloC();
    }
}

. I compiled it and then created header file. Then make the following HelloWorld.c file.

#include <stdio.h>
#include <jni.h>
#include "JavaToC.h"
JNIEXPORT void JNICALL Java_JavaToC_helloC(JNIEnv *env, jobject javaobj)
{
  printf("Hello World: From C");
  return;
}

I tried compiling this using "gcc -o libHelloWorld.so -shared -I/usr/java/include -I/usr/java/include/solaris HelloWorld.c -lc", but it gives the following result.

Text relocation remains                     referenced
    against symbol          offset  in file
.rodata (section)                   0x9         /var/tmp//cc.GaGrd.o
printf                              0xe         /var/tmp//cc.GaGrd.o
ld: fatal: relocations remain against allocatable but non-writable sections
collect2: ld returned 1 exit status

I'm working on Solaris 11, how can I solve this?


Solution

  • I cannot test this on a Solaris machine at the moment, but from http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/SPARC-Options.html

    -mimpure-text suppresses the “relocations remain against allocatable but non-writable sections” linker error message. However, the necessary relocations will trigger copy-on-write, and the shared object is not actually shared across processes. Instead of using -mimpure-text, you should compile all source code with -fpic or -fPIC.

    the solution seems to be to add the -fpic option to generate position-independent code.