当我想要共享库时,GCC 输出可执行 ELF 文件
我正在尝试使用 i686-elf 交叉编译器在 Cygwin 中构建共享库。代码非常简单:
int add(int a, int b) {
return a + b;
}
void _init() {
add(3, 4);
}
我使用以下命令进行编译:
i686-elf-gcc -fPIC -shared -nostdlib core.c -o libcore.so
这应该生成一个共享对象,对吗?但是 GCC 会输出一条关于无法找到 _start
符号的警告,该符号是可执行文件的入口点,而不是共享对象的入口点。此外,readelf 还表示以下内容:
$ readelf -a libcore.so
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
...
Type: EXEC (Executable file)
...
这里出了什么问题?
I'm trying to build a shared library in Cygwin using an i686-elf cross-compiler. The code is very simple:
int add(int a, int b) {
return a + b;
}
void _init() {
add(3, 4);
}
I'm compiling with the following command:
i686-elf-gcc -fPIC -shared -nostdlib core.c -o libcore.so
This should be producing a shared object, right? But GCC outputs a warning about not being able to find the _start
symbol, which is the entry point for executables, not shared objects. Furthermore, readelf
says the following:
$ readelf -a libcore.so
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
...
Type: EXEC (Executable file)
...
What's going wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题本质上是你的目标是 i686-elf,而没有人为该目标构建共享库。
-Wl,-shared
将为您提供标记为共享库的内容,但是您计划如何在裸机目标上加载共享库呢?What's going wrong is essentially that you're targeting i686-elf, and nobody builds shared libraries for that target.
-Wl,-shared
will give you something which is marked as a shared library, but how exactly do you plan to load a shared library on a bare-metal target?我相信裸机目标上的
-shared
是无操作(使编译器认为未指定该选项),因此编译器会构建可执行文件。从命令行上的info gcc
:...并向下滚动查看脚注:
I believe that
-shared
on bare metal targets is a no-op (making the compiler believe the option wasn't specified) and therefore the compiler builds an executable. Frominfo gcc
on the command line:... and scrolling down for footnotes: