如何正确使用 -fPIC 和 -fPIE GCC 选项

发布于 2025-01-19 10:36:55 字数 1148 浏览 2 评论 0原文

我有这样的目录结构,

dir1
    one.c
    two.c
dir2
    three_main.c
    four.c

我需要在dir1
中创建一个共享库libdir1.so。 并且可执行my_exedir2中的所有 c 文件中,以及libdir1.so

我正在创建< strong> .so 和可执行分为两个步骤。

.so step1:    gcc -c -fPIC -g -O2 -pthread -o one.o one.c
              gcc -c -fPIC -g -O2 -pthread -o two.o two.c
.so step2:    gcc -shared -g -O2 -pthread -o libdir1.so one.o two.o


exe step1:    gcc -c -fPIE -o three_main.o three_main.c
              gcc -c -fPIE -o four.o four.c
exe step2:    gcc -Wall -g -L -ldir1 -o my_exe three_main.o four.o

现在我的问题是...。

  1. 我应该使用 -fpic -fpie step1step2或在两个都?
  2. 我可以在创建 step2 中使用 -fpie 在创建 .so 文件中吗?
  3. 我可以在创建 step1 中使用 -fpic 在创建可执行文件中吗?
  4. 如何使用 relro 编译器选项来创建 .so 可执行文件

谢谢

I have a directory structure like this

dir1
    one.c
    two.c
dir2
    three_main.c
    four.c

I need to create a shared library libdir1.so out of all c files in dir1
and an executable my_exe out of all c files in dir2 along with the libdir1.so

I am creating the .so and the executable in two steps.

.so step1:    gcc -c -fPIC -g -O2 -pthread -o one.o one.c
              gcc -c -fPIC -g -O2 -pthread -o two.o two.c
.so step2:    gcc -shared -g -O2 -pthread -o libdir1.so one.o two.o


exe step1:    gcc -c -fPIE -o three_main.o three_main.c
              gcc -c -fPIE -o four.o four.c
exe step2:    gcc -Wall -g -L -ldir1 -o my_exe three_main.o four.o

Now my questions are....

  1. Should I use -fPIC and -fPIE in step1 or step2 or in both?
  2. Can I use -fPIE in step2 in creating the .so file?
  3. Can I use -fPIC in step1 in creating the executable?
  4. How can I use RELRO compiler option in creating both .so and executable?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

优雅的叶子 2025-01-26 10:36:55

我应该在步骤 1 或步骤 2 或两者中使用 -fPIC 和 -fPIE?

不。您当前构建 .so 和二进制文件的方式是正确/最佳的。

我可以在第 2 步创建 .so 文件时使用 -fPIE 吗?

你可以,但你不应该:-fPIE是一个编译时选项,并且你只在第2步中链接。如果将 -fPIE 传递给链接,它将被忽略。

我可以在创建可执行文件的步骤 1 中使用 -fPIC 吗?

您可以,但这会在可执行文件中创建次优代码。

如何使用 RELRO 编译器选项来创建 .so 和可执行文件?

添加-Wl,-z,relro

我应该将 -z,now 与 -Wl,-z,relro 一起使用才能有效吗?

是的:为了最大限度地强化,你应该这样做。没有 -z,now-Wl,-z,relro 并不是无效,而是不太有效。 详细信息

我可以对二进制文件和 .so 使用 relro 吗?

是的。

Should I use -fPIC and -fPIE in step1 or step2 or in both?

No. The way you currently build the .so and the binary is correct / optimal.

Can I use -fPIE in step2 in creating the .so file?

You could, but you shouldn't: -fPIE is a compile-time option, and you are only linking in step2. If you pass -fPIE to the link, it will be ignored.

Can I use -fPIC in step1 in creating the executable?

You could, but this will create sub-optimal code in the executable.

How can I use RELRO compiler option in creating both .so and executable?

Add -Wl,-z,relro.

should I be using -z,now along with -Wl,-z,relro to be effective?

Yes: for maximum hardening, you should. -Wl,-z,relro without -z,now is not ineffective, but it is less effective. Details.

can I use relro for both binary and .so?

Yes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文