gcc`-shared`选项如何影响输出? [英] How does gcc `-shared` option affect the output?

查看:126
本文介绍了gcc`-shared`选项如何影响输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从文件内容上讲,gcc -fPIC -shared src.cgcc -fPIC src.c的输出有什么区别?

Technically, in terms of file content, what is the difference between the output of gcc -fPIC -shared src.c and gcc -fPIC src.c?

假定在src.c中定义了int main(int, char**),以便两个编译都成功.但是按预期执行gcc -shared src.c生成的a.out会产生以下错误:

Assume that int main(int, char**) is defined in src.c so that both compilations succeed. But executing the a.out generated by gcc -shared src.c, as expected, gave the following error:

-bash: ./a_shared.out: cannot execute binary file

即使有main函数也是如此.

此外,如何使用otoolobjdump之类的工具检查输出文件中的差异?

Also, how can I inspect the difference in the output files using tools like otool or objdump?

非常感谢.

推荐答案

共享库和可执行文件使用相同的格式:它们都是可加载的映像.但是,

Shared libraries and executables use the same format: they are both loadable images. However,

  1. 共享库通常与位置无关,而可执行文件则通常与位置无关.这会影响代码生成:对于与位置无关的位置,您必须使用相对地址加载全局变量或跳转到函数.

  1. Shared libraries are usually position-independent, executables are often not. This affects code generation: for position-independent, you have to load globals or jump to functions using relative addresses.

可执行文件具有一个入口",这是执行开始的地方.通常不是 main(),因为main()是一个函数,并且函数会返回,但是执行绝不应从入口点返回.

Executables have an "entry point" which is where execution starts. This is usually not main(), because main() is a function, and functions return, but execution should never return from the entry point.

现在,这不能回答有关-shared的问题.您可以使用-v标志来询问GCC.这是我的系统在不使用-shared和使用-shared进行调用之间的区别.

Now, this doesn't answer the question about what -shared does. You can ask GCC by using the -v flag. Here are the differences on my system between an invocation without and with -shared.

-dynamic-linker
/lib64/ld-linux-x86-64.so.2
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o
/usr/lib/gcc/x86_64-linux-gnu/4.7/crtbegin.o
/usr/lib/gcc/x86_64-linux-gnu/4.7/crtend.o

collect2-shared的参数:

Parameters for collect2 with -shared:

-shared
/usr/lib/gcc/x86_64-linux-gnu/4.7/crtbeginS.o
/usr/lib/gcc/x86_64-linux-gnu/4.7/crtendS.o

观察

似乎代码生成没有受到影响:您仍然必须使用-fpic-fPIC.

您可以看到,仅当链接可执行文件时,才包含crt1.o("C运行时").使用nm,我们可以找到它包含的内容:

You can see that crt1.o (the "C runtime") is only included when linking the executable. Using nm, we can find out what it contains:

$ nm /usr/lib/x86_64-linux-gnu/crt1.o
0000000000000000 R _IO_stdin_used
0000000000000000 D __data_start
                 U __libc_csu_fini
                 U __libc_csu_init
                 U __libc_start_main
0000000000000000 T _start
0000000000000000 W data_start
                 U main

因此您可以看到它似乎定义了与stdin_start(这是入口点)有关的东西,并且它具有对main的未定义引用.

So you can see it seems to define something to do with stdin, as well as _start (which is the entry point), and it has an undefined reference to main.

我不确定其余文件是什么,但是至少您知道如何找到它们,可以四处浏览,或者根据需要查看源代码.

I'm not sure what the rest of the files are, but at least you know how to find them and you can poke around, or look at the source code if you like.

这篇关于gcc`-shared`选项如何影响输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆