mainがputsを呼ぶHello World

追記: いや、ちょっと待てx86だと動かないような。
追記: やっぱ動かない…。x86-64とかだと動くかも。


http://d.hatena.ne.jp/yupo5656/20060828/p1
http://d.hatena.ne.jp/shinichiro_h/20060829#1156788161

負けてられないので(何の勝負だ)参加。


mainからputsを呼びだすというごくごくありきたりなHello world

$ cat hello.c

main() { puts(); }

#include <sys/mman.h>
__attribute__((constructor)) init() {
  mmap( 0, 4096, PROT_READ|PROT_WRITE, MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE, 0, 0 );
  strcpy( 1, "Hello World");
}

$ gcc hello.c
hello.c: In function ‘init’:
hello.c:6: 警告: incompatible implicit declaration of built-in function ‘strcpy’
hello.c:6: 警告: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast
$ ./a.out 
Hello World
  • mainからputsを呼ぶので、よくわかります。
  • ポータブルです。PPCでも動きます。ポータブルじゃないです。x86だと動きません。


とりあえず、NULLにもmmapできるんだなー。とかいうような。

$ ./a.out ero
ello World

エロワールド。

/*Hello World^@*/
main() { puts(); }

#include <sys/mman.h>
#include <fcntl.h>
__attribute__((constructor)) init() {
  mmap( 0, 4096, PROT_READ, MAP_FIXED|MAP_PRIVATE, open(__FILE__,O_RDONLY), 0 );
}

うーん。^@は「C-q@」で…ってちょっと無理すぎるか…