程序的编译和链接

Posted by hello vbnmjj on January 9, 2024

从c代码到可执行文件

编译
    由c语言代码生成汇编代码
    #include <stdio.h>

    int main(){

            printf("hello");
            return 0;
    }

汇编
    由汇编代码生成目标链接
链接
    将多个机器码的目标文件链接成一个可执行文件

linux 查看文件的16进制表示

    xxd 命令

使用命令gcc -S test.c

            .file   "test.c"
            .text
            .section        .rodata
    .LC0:
            .string "hello"
            .text
            .globl  main
            .type   main, @function
    main:
    .LFB0:
            .cfi_startproc
            pushq   %rbp
            .cfi_def_cfa_offset 16
            .cfi_offset 6, -16
            movq    %rsp, %rbp
            .cfi_def_cfa_register 6
            leaq    .LC0(%rip), %rax
            movq    %rax, %rdi
            movl    $0, %eax
            call    printf@PLT
            movl    $0, %eax
            popq    %rbp
            .cfi_def_cfa 7, 8
            ret
            .cfi_endproc
    .LFE0:
            .size   main, .-main
            .ident  "GCC: (Debian 13.2.0-5) 13.2.0"
            .section        .note.GNU-stack,"",@progbits

什么是可执行文件?

基础汇编指令

mov est,src
mov eax [0040d011h] ; []代表取值

lea reg,src  ;把源操作数的有效地址传送给指定的寄存器

push value ;把目标值压栈,同时SP指针-1字长
pop dest ;把栈顶的值弹出至目的的存储位置,同时SP指针 +1字长
leave    ;在函数返回时,恢复父函数栈帧的指令
    等效于mov esp,ebp
          pop ebp
ret      ;在函数返回时,控制程序执行流返回父函数的指令

两种汇编格式