has text relocation issue with so shared library

 

1. What is the so shared library?

   so shared library is a function library

 

2.so shared library has no main function

    The code to write a so is as follows, and it becomes a .so file after compilation

  

int max(int a,int b){
    if(a>b)
        return a;
    else
        return b;
}
int add(int a,int b){
    return a+b;
}

 

3.java calls so library

public class Hello{

public native static int add(int x,int y); //declare the native method in the java code

static {
    System.load("/home/lance/main.so"); //Load so file with absolute path }

public static void main(String[] args){  
    System.out.println(add(1,1));    
}

}

 

4. The reason for has text relocation is that the so library is not compiled with PIC, not location independent code

 

A key purpose of the so library is to enable multiple processes to share the same code in memory and save memory resources.

How to do it?

1. Pre-specify the loaded address range for each so library, and then ask the loader to always load the so library to the specified location. This approach is simple but creates some serious problems. If a process does not use a library, the corresponding address range will still be reserved, and the memory usage efficiency is very low. Management is also difficult. We must ensure that there is no overlap between the reserved address blocks. Whenever a library is modified, we also have to make sure that it can be put back in its original location, otherwise, we have to find a new location for it. When we create a new library, we also have to find a suitable space for it, a lot of useless memory holes caused by fragmentation of the address space. To make matters worse, different systems allocate memory for dynamic libraries differently, which makes it more difficult to manage.
2. Compile the dynamic library so that it can be loaded anywhere without modification by the linker. Such code is called position independent code (PIC). The GNU build system can generate PIC code by specifying the -fPIC option.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326195653&siteId=291194637