編譯一個小的可執行程序

  • 一個儘量小的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$
      
  • 發表評論
    所有評論
    還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
    相關文章