2017-01-10 10 views
0

をコンパイルするには、私は、ASMコードを持っています。私は実行可能ファイルを32ビットにします。動的リンクに 私がやる:私が何をしたいか静的にlibcのCコードとASMコード

nasm -f elf32 asm.asm ; this gives me asm.o 
gcc -m32 -Wall -c c_code.c ; this gives me c_code.o 
ld c_code.o asm.o -melf_i386 -L /usr/lib/ -lc -I /lib/ld-linux.so.2 ; this gives me a.out which runs fine and weights 5601 bytes. 

は、静的にリンクlibcのです。

gcc -o a2.out -m32 -static -m32 asm.o c_code.o 

そして、私が取得エラー:私は、次の手順を実行し

asm.o: In function `_start': 
asm.asm:(.text+0x0): multiple definition of `_start' 
/usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../lib64/crt1.o:(.text+0x0): 
first defined here  
collect2: error: ld returned 1 exit status 

それから私は、ASMコードでメインに_startを変更し、全部のリンク罰金! lddは "動的実行可能ではない"を示します。しかし、ファイルは721067バイトの重み付けを作成しました!私はそれが静的に多くの不必要なコードをコンパイルすると思います。 私の最初の質問は次のとおりです。

1)必要なprintfとexit関数のためにlibcのみを静的にリンクするにはどうすればよいですか?

私は

gcc -m32 -o a3.out -lc asm.o c_code.o ; ASM file has main instead of _start 

をしようとすると、私はその重み7406バイトのファイルを取得します。 lddは5601バイトを重み付けするa.outと同じ動的ライブラリを表示します。

2)なぜその違いがありますか?私のコードで_startをmainと "接続"するいくつかの追加コードのようです... 3)gccとldとのリンクの違いは何ですか?

ありがとうございました!

答えて

0

1) How can I link statically only libc for the required printf and exit functions?

crt1.o初とcrtend.oを追加しないであろう-nostartfiles -static -nostdlib -lcでコンパイルしてみます。しかし、Glibcの初期化コードがすべて無効になるので、Glibcの機能がうまく動作しないことに注意してください。

2) Why is that difference? Looks like some additional code that "connects" _start with main in my code...

GCCは、初期化を実行する開始ファイル(crt*.o)を追加します。詳細については、多くのオンライン記事をご覧ください(例:this one)。

3) What is the difference between linking with gcc and ld?

は、すでに上記の答えが、一般的に、あなたは gcc -vを実行し、LDの(あるいはcollect2はの)引数を調べることができます。

+0

ありがとうございます! -nostartfilesは、サイズを5749バイトに縮小します。しかし、lddはまだlibc.so.6を表示します。これは、libcが依然として動的にリンクされていることを意味します。 – alexeik

+0

@alexeik '-nostdlib'も渡します。 – fuz

+0

申し訳ありませんが、静的な部分、更新された返信に気付かなかった。 – yugr

関連する問題