简体   繁体   English

当我想要一个共享库时,GCC输出一个可执行的ELF文件

[英]GCC outputs an executable ELF file when I want a shared library

I'm trying to build a shared library in Cygwin using an i686-elf cross-compiler. 我正在尝试使用i686-elf交叉编译器在Cygwin中构建一个共享库。 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. 但GCC会输出一个警告,指出无法找到_start符号,这是可执行文件的入口点,而不是共享对象。 Furthermore, readelf says the following: 此外, 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)
  ...

What's going wrong here? 这里出了什么问题?

What's going wrong is essentially that you're targeting i686-elf, and nobody builds shared libraries for that target. 出了什么问题本质上是你的目标是i686-elf,没有人为这个目标构建共享库。 -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? -Wl,-shared将为您提供标记为共享库的内容,但您打算如何在裸机目标上加载共享库?

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. 我相信 - 裸机目标上的-shared是一个无操作(使编译器认为没有指定选项),因此编译器构建了一个可执行文件。 From info gcc on the command line: 从命令行上的info gcc

'-shared'
    Produce a shared object which can then be linked with other
    objects to form an executable.  Not all systems support this
    option.  For predictable results, you must also specify the same
    set of options that were used to generate code ('-fpic', '-fPIC',
    or model suboptions) when you specify this option.(1)

... and scrolling down for footnotes: ...向下滚动脚注:

  (1) On some systems, 'gcc -shared' needs to build supplementary stub
  code for constructors to work.  On multi-libbed systems, 'gcc -shared'
  must select the correct support libraries to link against.  Failing to
  supply the correct flags may lead to subtle defects.  Supplying them in
  cases where they are not necessary is innocuous.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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