システムコールだけでHello, world

リンカのお勉強中デス。

Binary Hacksのhack#25 「glibcを使わないでHello World」が面白そうだったので、アセンブリでやってみました。

.data

hello:
    .ascii "Hello, World!!"

.text
.global _start

_start:
    movl $4, %eax
    movl $1, %ebx
    movl $hello, %ecx
    movl $14, %edx
    int $0x80

    movl $1, %eax
    movl $0, %ebx
    int $0x80
  • linuxではmainの前にエントリポイント_startが呼ばれる。ld -eでエントリポイントを変更できる。
  • eaxはシステムコール番号を入れる。1がexit,4がwrite
  • ebx,ecx,edxの順に引数を入れる。
  • int 0x80がシステムコール

コンパイルしてみよう〜

オブジェクトファイル作って、

% as -o hello.o hello.s

リンカを通す。

% ld -o hello hello.o
% ./hello

実行〜

Hello, World!!

おぉぉぉぉ。

ファイルサイズを見てみると、

% wc -c hello
614 hello

614バイト。ボクノスよりもおっきいです。高級です。


おっきいので、Binary Hacks見ながらダイエットしてみます。

% strip -s hello
% wc -c hello
352 hello
% ./hello
Hello, World!!

おぉ、はんぶん。Binary Hacksよりちっちゃくなったので満足です。ばんざーい。

と思ったけど

続きがありました。

2006-11-12 - memologue

132バイト・・・

お、解説発見

ELF Golf

58バイト・・・

僕も、真似して、167バイトまで削れました。これがddの限界。

やっぱリンカを学ばねば。

紳士のスポーツに参加しよう。

elfについて調べてみる

調べてみると、

readelfの殆どが10進数で書かれてるので、基数変換で死にそうだ・・・。