我如何使用 objdump 之类的工具判断目标文件是否已使用 -fPIC 构建?
如何使用 objdump
等工具判断目标文件是否已使用 -fPIC
构建?
How can I tell, with something like objdump
, if an object file has been built with -fPIC
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
答案取决于平台。在大多数平台上,如果 from 的输出
为空,则说明
foo.o
未使用-fPIC
进行编译,或者foo.o
未使用-fPIC
进行编译。包含-fPIC
重要的任何代码。The answer depends on the platform. On most platforms, if output from
is empty, then either
foo.o
was not compiled with-fPIC
, orfoo.o
doesn't contain any code where-fPIC
matters.我只需在 PowerPC 目标上执行此操作即可找到在没有 -fPIC 的情况下构建的共享对象 (.so)。我所做的是运行 readelf -d libMyLib1.so 并查找 TEXTREL。如果您看到 TEXTREL,则说明组成 .so 的一个或多个源文件不是使用 -fPIC 构建的。如有必要,您可以将 readelf 替换为 elfdump。
例如,
为了帮助人们寻找解决方案,我在运行可执行文件时遇到的错误是:
我不知道此信息是否适用于所有体系结构。
来源:blogs.oracle.com/rie
I just had to do this on a PowerPC target to find which shared object (.so) was being built without -fPIC. What I did was run readelf -d libMyLib1.so and look for TEXTREL. If you see TEXTREL, one or more source files that make up your .so were not built with -fPIC. You can substitute readelf with elfdump if necessary.
E.g.,
And to help people searching for solutions, the error I was getting when I ran my executable was this:
I don't know whether this info applies to all architectures.
Source: blogs.oracle.com/rie
我假设,您真正想知道的是共享库是否由使用 -fPIC 编译的目标文件组成。
正如已经提到的,如果有 TEXTREL,则不使用 -fPIC。
有一个很棒的工具,称为 scanelf,它可以向您显示导致 .text 重定位的符号。
更多信息请参阅如何定位和修复 .text 重定位 TEXTREL。
I assume, what you really want to know is whether or not a shared library is composed from object files compiled with -fPIC.
As already mentioned, if there are TEXTRELs, then -fPIC was not used.
There is a great tool called scanelf which can show you the symbols that caused .text relocations.
More information can be found at HOWTO Locate and Fix .text Relocations TEXTRELs.
-fPIC 意味着代码将能够在与编译地址不同的地址中执行。
为此,反装器将如下所示......
现在在 axe 中,我们有一个偏移量,我们需要将其添加到对内存的任何访问中。
-fPIC means that code will be able to execute in addresses different form the address that was compile for.
To do it , disasambler will look like this....
now in ax we have an offset that we need to add to any access to memory.
这应该在大多数情况下都有效。
This should work most of the time.
区分程序是否是通过 -fPIC 选项生成的另一个选项:
前提是您的代码在编译时启用了 -g3 -gdwarf-2 选项。
其他 gcc 调试格式也可能包含宏信息:
注意以下 $'..' 语法假定 bash
这样的方法有效,因为 gcc 手册声明如果使用 -fpic,则 PIC 定义为 1 , 和
如果使用-fPIC,PIC为2。
通过检查GOT的上述答案是更好的方法。因为我猜 -g3 -gdwarf-2 的预请求很少被使用。
Another option to distinguish whether your program is generated wit -fPIC option:
provided that your code has -g3 -gdwarf-2 option enabled when compiling.
other gcc debug format may also contains the macro info:
Note the following $'..' syntax is assumes bash
such a method works because gcc manual declares that if -fpic is used, PIC is defined to 1, and
if -fPIC used, PIC is 2.
The above answers by checking the GOT is the better way. Because the prerequest of -g3 -gdwarf-2 I guess seldom being used.
来自Linux 编程接口:
但是,上面的引用和这个问题的任何答案都不适用于 x86_64。
我在 x86_64 Ubuntu 机器上观察到的是,无论是否指定
-fPIC
,都会生成fPIC.o
。这没有区别,
my_so.o
和my_so_fpic.o
都可以用来创建共享库。为了生成非fpic目标文件,我在如何测试 Linux 二进制文件是否编译为位置无关代码? .
这有效,
并
给出错误:
无法使用非 pic
.o
创建共享库。From The Linux Programming Interface:
However, neither above quoting nor any answer of this question works for x86_64.
What I've observed on my x86_64 Ubuntu machine is that, whether specifying
-fPIC
or not, it would generate fPIC.o
. That ishas no difference and both
my_so.o
andmy_so_fpic.o
can be used to create a shared library.In order to generate non fpic object file, I found a gcc flag called
-fno-pic
in the first comment of How to test whether a Linux binary was compiled as position independent code? .This works,
and
gives error:
can not create a shared library with non pic
.o
.