2011-05-07 25 views
5

我想在使用i686-elf交叉編譯器的Cygwin中構建一個共享庫。該代碼非常簡單:當我想要一個共享庫時,GCC輸出一個可執行的ELF文件

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) 
    ... 

這是怎麼回事?

回答

2

問題的根本在於你的目標是i686-elf,沒有人爲該目標建立共享庫。 -Wl,-shared會給你標記爲共享庫的東西,但你打算如何在裸金屬目標上加載共享庫?

+0

這工作很好,謝謝。爲了回答你的問題,我正在開發一個加載可重定位ELF模塊的引導程序。 – 2011-05-07 14:19:25

0

我相信bare裸機上的-shared是no-op(使編譯器相信這個選項沒有被指定),因此編譯器會生成一個可執行文件。從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) 

...並滾動腳註:

(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. 
相關問題