Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why compile Android kernel module with -fno-pic?

I often read that Android kernel modules have to be compiled with -fno-pic to work. Is this specific to the ARM architecture, or why don't/(when do) kernel modules for x86 need to be compiled with that flag?

like image 812
jckuester Avatar asked Dec 05 '14 03:12

jckuester


People also ask

Why do we need kernel modules?

Using modules can save memory, because they are loaded only when the system is actually using them. All parts of the base kernel stay loaded, in real storage, not just virtual storage. Modules are much faster to maintain and debug.

What is the use of kernel in Android?

In a nutshell, a kernel is the core program that manages your phone's CPU resources, the system memory, and the system devices (including the file systems and networking). It is also responsible for managing all the processes or tasks that are running on your smartphone.

What is the use of modprobe?

Use the modprobe command to add or remove modules on Linux. The command works intelligently and adds any dependent modules automatically. The kernel uses modprobe to request modules. The modprobe command searches through the standard installed module directories to find the necessary drivers.

What do insmod and rmmod do?

The rmmod command is used to remove a module from the kernel. Most of the users use modprobe with the -r option instead of using rmmod. The rmmod command is extremely simple. You only need to give it the name of a module that you want to unload, and it will remove it.


1 Answers

According to https://gcc.gnu.org/onlinedocs/gcc-6.1.0/gcc/Code-Gen-Options.html, -fno-pic is the negative form of the -fpic parameter. From the same link:

-fpic Generate position-independent code (PIC) suitable for use in a shared library, if supported for the target machine. Such code accesses all constant addresses through a global offset table (GOT). The dynamic loader resolves the GOT entries when the program starts (the dynamic loader is not part of GCC; it is part of the operating system). If the GOT size for the linked executable exceeds a machine-specific maximum size, you get an error message from the linker indicating that -fpic does not work; in that case, recompile with -fPIC instead. (These maximums are 8k on the SPARC, 28k on AArch64 and 32k on the m68k and RS/6000. The x86 has no such limit.) Position-independent code requires special support, and therefore works only on certain machines. For the x86, GCC supports PIC for System V but not for the Sun 386i. Code generated for the IBM RS/6000 is always position-independent.

When this flag is set, the macros __pic__ and __PIC__ are defined to 1.

So -fno-pic means something like "Do not use position-independent code (PIC)."

But why?

Well, by looking at https://developer.arm.com/products/software-development-tools/hpc/documentation/note-about-building-position-independent-code-pic-on-aarch64, we find that:

Using the -fpic compiler flag with GCC compilers on AArch64 causes the compiler to generate one less instruction per address computation in the code, and can provide code size and performance benefits. However, it also sets a limit of 32k for the Global Offset Table (GOT), and the build can fail at the executable linking stage because the GOT overflows.

So, in the end, it seems like -fno-pic is more of a precaution than a real need. This, of course, is a guess and there might be more things involved.

like image 199
Alexandre Schmidt Avatar answered Oct 07 '22 11:10

Alexandre Schmidt