LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 920|回复: 0

编译自己的迷你ELF文件

[复制链接]
发表于 2006-9-19 11:03:48 | 显示全部楼层 |阅读模式
原文:http://blog.chinaunix.net/u/8057/showart.php?id=172110

; 最近研究ELF格式,hack了一把。

; 参考文档:
; 1。 ELF_format.pdf http://www.skyfree.org/linux/references/ELF_Format.pdf
; 2。A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux
; http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html

; 各文件:
;;; **************************************************
; tinyso1.S
;;; **************************************************

  1. .global _start
  2. _start:
  3.     xor %eax, %eax
  4.     inc %eax
  5.     movw $filesize,%bx
  6.     int  $0x80
  7. .equ filesize, . - _start
复制代码


;;; **************************************************
; tinyso2.S
;;; **************************************************

  1. /* ELF Header */
  2. ehdr:
  3.     /* e_ident */
  4.     .byte 0x7F,'E,'L,'F
  5.     .byte 1 # EI_CLASS : ELFCLASS32
  6.     .byte 1 # EI_DATA  : ELFDATA2LSB
  7.     .byte 1 # EI_VERSION
  8.     .rept 9
  9.         .byte 0
  10.     .endr

  11.     .word 2        # e_type : ET_EXEC
  12.     .word 3        # e_machine : EM_386
  13.     .int  1        # e_version
  14.     .int  _start   # e_entry
  15.     .int  phdr_off # e_phoff
  16.     .int  0        # e_shoff
  17.     .int  0        # e_flags
  18.     .word ehdrsize # e_ehsize
  19.   .word phdrsize # e_phentsize
  20.     .word 1        # e_phnum
  21.     .word 0        # e_shentsize
  22.     .word 0        # e_shnum
  23.     .word 0        # e_shstrndx
  24. .equ ehdrsize, . - ehdr

  25. /* Program Headher */
  26. .equ phdr_off, . - ehdr
  27. phdr:
  28.     .int  1          # p_type : PT_LOAD
  29.     .int  0          # p_offset
  30.     .int  ehdr       # p_vaddr
  31.     .int  ehdr       # p_paddr (ignored)
  32.     .int  filesize   # p_filesz
  33.     .int  filesize   # p_memsz
  34.     .int  5          # p_flags : r-x
  35.     .int  0x1000     # p_align
  36. .equ phdrsize, . - phdr

  37. /* Code */
  38. .global _start
  39. _start:
  40.     xor %eax,%eax
  41.     inc %eax
  42.     movw $filesize,%bx
  43.     int  $0x80

  44. .equ filesize, . - ehdr
复制代码


;;; **************************************************
; tinyso3.S
;;; **************************************************

  1. /* ELF Header 16 bytes, with embed CODE into preserved area */
  2. ehdr:
  3.     /* e_ident */
  4.     .byte 0x7F,'E,'L,'F
  5.     .byte 1 # EI_CLASS : ELFCLASS32
  6.     .byte 1 # EI_DATA  : ELFDATA2LSB
  7.     .byte 1 # EI_VERSION
  8.     .global _start
  9. /*
  10. * <HACK> 1: embed CODE into preserved area
  11. * exactly, 9 bytes!
  12. */
  13. _start:
  14.     xor %eax,%eax        # 2 bytes
  15.     inc %eax             # 1 bytes
  16.     movw $filesize,%bx   # 4 bytes
  17.     int  $0x80           # 2 bytes

  18.     .word 2        # e_type : ET_EXEC
  19.     .word 3        # e_machine : EM_386
  20.     .int  1        # e_version
  21.     .int  _start   # e_entry
  22.     .int  phdr_off # e_phoff
  23.     .int  0        # e_shoff
  24.     .int  0        # e_flags
  25.     .word ehdrsize # e_ehsize
  26.   .word phdrsize # e_phentsize

  27. /* Program Headher */
  28. .equ phdr_off, . - ehdr
  29. phdr:
  30. /*
  31. * <HACK> 2 : overlap two section
  32. * .word 1        # e_phnum
  33. * .word 0        # e_shentsize
  34. * .word 0        # e_shnum
  35. * .word 0        # e_shstrndx
  36. */
  37.     .int  1          # p_type : PT_LOAD
  38.     .int  0          # p_offset
  39. .equ ehdrsize, . - ehdr
  40.     .int  ehdr       # p_vaddr
  41.     .int  ehdr       # p_paddr (ignored)
  42.     .int  filesize   # p_filesz
  43.     .int  filesize   # p_memsz
  44.     .int  5          # p_flags : r-x
  45.     .int  0x1000     # p_align
  46. .equ phdrsize, . - phdr

  47. .equ filesize, . - ehdr
复制代码


  1. Makefile:

  2. all:tinyso1 tinyso2 tinyso3

  3. tinyso1: tinyso1.S
  4.     @echo ----------   $@  --------------
  5.     gcc -nostdlib $< -o $@
  6.     strip -s $@
  7.     wc -c $@
  8.     @echo ---------------------------------

  9. tinyso2: tinyso2.S
  10.     @echo -----------  $@  ---------------
  11.     gcc -c $<
  12.     ld tinyso2.o --oformat binary -o $@
  13.     @echo ---------------------------------

  14. tinyso3: tinyso3.S
  15.     @echo -----------  $@  ----------------
  16.     gcc -c $<
  17.     ld tinyso3.o --oformat binary -o $@
  18.     @echo ---------------------------------

  19. clean:
  20.     rm -f *.o tinyso1 tinyso2 tinyso3
复制代码



效果:

  1. hellwolf@cocteau#pts/9%J0S2:tinyso$wc -c tinyso?
  2. 264 tinyso1
  3. 93 tinyso2
  4. 76 tinyso3
  5. 433 total
  6. hellwolf@cocteau#pts/9%J0S2:tinyso$for i in $(seq 1 3);do ./tinyso$i;echo $?;done
  7. 9
  8. 93
  9. 76
  10. hellwolf@cocteau#pts/9%J0S2:tinyso$
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表