LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-03-2006, 03:17 AM   #1
George2
Member
 
Registered: Oct 2003
Posts: 354

Rep: Reputation: 30
about -fpic/-fPIC option for gcc


Hello everyone,


I am confused about the meaning and function of -fpic and -fPIC option of gcc. I am more confused when after reading the manual of gcc (for example, what means PIC, GOT, constant addresses, etc.). Could anyone explain to me what are their functions in easy to understand words?

Here are the descriptions from gcc manual, which makes me confused. :-)

--------------------
-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 and 32k on the m68k and
RS/6000. The 386 has no such limit.)
Position-independent code requires special support, and therefore works only on
certain machines. For the 386, GCC supports PIC for System V but not for the
Sun 386i. Code generated for the IBM RS/6000 is always position-independent.

-fPIC If supported for the target machine, emit position-independent code, suitable
for dynamic linking and avoiding any limit on the size of the global offset table.
This option makes a difference on the m68k, PowerPC and SPARC.
Chapter 3: GCC Command Options 193
Position-independent code requires special support, and therefore works only
on certain machines.
--------------------


thanks in advance,
George
 
Old 09-03-2006, 03:47 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

The basic issue is:
1. If you've got a shared library

2. How can you share static data in the library among multiple processes, each of which
will map that data to completely different addresses?

"Position Independent Code" ("PIC") is one common solution.

Here are a couple of links that might help explain the details a bit better:
http://docs.hp.com/en/B2355-90655/ch07s03.html
http://www.iecc.com/linker/linker10.html
http://en.wikipedia.org/wiki/Shared_library
http://en.wikipedia.org/wiki/Position_Independent_Code

'Hope that helps .. PSM
 
Old 09-04-2006, 01:38 AM   #3
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Thank you paulsm4,


Quote:
Originally Posted by paulsm4
Hi -

The basic issue is:
1. If you've got a shared library

2. How can you share static data in the library among multiple processes, each of which
will map that data to completely different addresses?

"Position Independent Code" ("PIC") is one common solution.

Here are a couple of links that might help explain the details a bit better:
http://docs.hp.com/en/B2355-90655/ch07s03.html
http://www.iecc.com/linker/linker10.html
http://en.wikipedia.org/wiki/Shared_library
http://en.wikipedia.org/wiki/Position_Independent_Code

'Hope that helps .. PSM
I have read some of the materials. Seems that -fpic generates PIC and -fPIC does not generate PIC at all, is my understanding correct?

But if -fPIC does not generate PIC in shared library, how could other processes use the shared library? Since the address can not be decided until the execution of a process (so, non-PIC code, for example, specific address based on virtual memory, is not suitable).


regards,
George

Last edited by George2; 09-04-2006 at 01:40 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
about -00 option of gcc George2 Programming 3 08-29-2006 03:28 PM
gcc -I option vkmgeek Programming 18 05-16-2006 06:27 PM
what does the option -s of gcc mean ? hongxing Linux - Software 3 11-28-2005 08:12 PM
What does 'recompile with -fPIC' mean cocteau Linux - Software 1 03-31-2005 01:32 PM
gcc option -E use. niverson Programming 2 10-11-2004 04:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration