[英]What does -fPIC mean when building a shared library?
我知道“ -fPIC
”選項與解析地址和各個模塊之間的獨立性有關,但我不確定它的真正含義。 你可以解釋嗎?
PIC 代表位置無關代碼。
引用man gcc
:
如果目標機器支持,則發出與位置無關的代碼,適用於動態鏈接並避免對全局偏移表的大小進行任何限制。 此選項在 AArch64、m68k、PowerPC 和 SPARC 上有所不同。
在那些提到的體系結構上構建共享對象 (*.so) 時使用它。
f
是“控制代碼生成中使用的接口約定”選項的 gcc 前綴
該PIC
代表“位置無關的代碼”,它是一個專業化fpic
的M68K和SPARC。
編輯:在閱讀了 0x6adb015 引用的文檔的第 11 頁以及coryan的評論后,我做了一些更改:
此選項僅對共享庫有意義,並且您告訴操作系統您正在使用全局偏移表 GOT。 這意味着您所有的地址引用都與 GOT 相關,並且代碼可以在多個進程之間共享。
否則,如果沒有這個選項,加載器將不得不自己修改所有的偏移量。
不用說,我們幾乎總是使用 -fpic/PIC。
man gcc
說:
-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 and the SPARC. Position-independent code requires special support, and therefore works only on certain machines.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.