Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC -fPIC option

I have read about GCC's Options for Code Generation Conventions, but could not understand what "Generate position-independent code (PIC)" does. Please give an example to explain me what does it mean.

like image 742
Narek Avatar asked Mar 15 '11 12:03

Narek


People also ask

What does GCC option mean?

When you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The "overall options" allow you to stop this process at an intermediate stage. For example, the -c option says not to run the linker. Then the output consists of object files output by the assembler.

What is use of GCC?

It is a compiler system for the various programming languages. It is mainly used to compile the C and C++ programs. It takes the name of the source program as a necessary argument; rest arguments are optional such as debugging, warning, object file, and linking libraries. GCC is a core component of the GNU toolchain.

What is Wall option in GCC?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.

What is compile option?

A compiler option (also known as a switch) is an optional string of one or more alphanumeric characters preceded by a dash (-) (Linux* and Mac OS*) or a forward slash ( / ) (Windows*). Some options are on by default when you invoke the compiler.


2 Answers

Position Independent Code means that the generated machine code is not dependent on being located at a specific address in order to work.

E.g. jumps would be generated as relative rather than absolute.

Pseudo-assembly:

PIC: This would work whether the code was at address 100 or 1000

100: COMPARE REG1, REG2 101: JUMP_IF_EQUAL CURRENT+10 ... 111: NOP 

Non-PIC: This will only work if the code is at address 100

100: COMPARE REG1, REG2 101: JUMP_IF_EQUAL 111 ... 111: NOP 

EDIT: In response to comment.

If your code is compiled with -fPIC, it's suitable for inclusion in a library - the library must be able to be relocated from its preferred location in memory to another address, there could be another already loaded library at the address your library prefers.

like image 80
Erik Avatar answered Sep 20 '22 23:09

Erik


I'll try to explain what has already been said in a simpler way.

Whenever a shared lib is loaded, the loader (the code on the OS which load any program you run) changes some addresses in the code depending on where the object was loaded to.

In the above example, the "111" in the non-PIC code is written by the loader the first time it was loaded.

For not shared objects, you may want it to be like that because the compiler can make some optimizations on that code.

For shared object, if another process will want to "link" to that code it must read it to the same virtual addresses or the "111" will make no sense. But that virtual-space may already be in use in the second process.

like image 26
Roee Gavirel Avatar answered Sep 20 '22 23:09

Roee Gavirel