繁体   English   中英

构建共享库时 -fPIC 是什么意思?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM