编译一个小的可执行程序

  • 一个尽量小的a.out

    • 面向syscall编程

      • C函数之类的,环境之类的都自己实现。
      • 完全的自给自足。
    • 编译链接

      • 按照文件格式进行。

      • 最小的应该是64bit.大小

  • 案例

    • 代码test.c

      int main() {
         asm("movl $42, %ebx \n\t"
             "movl $1 , %eax \n\t"
             "int $0x80      \n\t");
      }
      
    • 编译makefile

      .PHONY:all clean
      
      ENTRY=-e main
      
      all:
             gcc -fno-builtin -c test.c -o test.o -g
             ld -T link.lds -g $(ENTRY)
      
      clean:
             rm -f test.o *.out
      
    • 链接脚本

      OUTPUT(ad.out)
      INPUT(test.o)
      SECTIONS
      {
        .text 0x400404 : { *(.text) }
      }
      
    • 完整执行

      chen@chen:~/cfile/ldscript$ cat link.lds
      OUTPUT(ad.out)
      INPUT(test.o)
      SECTIONS
      {
        .text 0x400404 : { *(.text) }
        empty : { *(.helloworld) ; good = . ; }
      }
      chen@chen:~/cfile/ldscript$ cat makefile
      .PHONY:all clean
      
      ENTRY=-e main
      
      all:
             gcc -fno-builtin -c test.c -o test.o -g
             ld -T link.lds -g $(ENTRY)
      
      clean:
             rm -f test.o *.out
      chen@chen:~/cfile/ldscript$ cat test.c
      int main() {
         asm("movl $42, %ebx \n\t"
             "movl $1 , %eax \n\t"
             "int $0x80      \n\t");
      }
      chen@chen:~/cfile/ldscript$ make clean
      rm -f test.o *.out
      chen@chen:~/cfile/ldscript$ ls
      link.lds  makefile  test.c
      chen@chen:~/cfile/ldscript$ make
      gcc -fno-builtin -c test.c -o test.o -g
      ld -T link.lds -g -e main
      chen@chen:~/cfile/ldscript$ ./ad.out
      chen@chen:~/cfile/ldscript$
      
  • 發表評論
    所有評論
    還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
    相關文章