Nice programing

GCC에서 생성 한 asm을 어떻게 컴파일합니까?

nicepro 2020. 11. 10. 22:18
반응형

GCC에서 생성 한 asm을 어떻게 컴파일합니까?


나는 약간의 asm 코드를 가지고 놀고 있는데 무언가가 나를 괴롭힌다.

나는 이것을 컴파일한다 :

#include <stdio.h>

int main(int argc, char** argv){
  printf("Hello World\n");
  return 0;
}

gcc file.c -S -o file.S이 ASM 코드의 좋은 작은 조각을 생성합니다 :

    .cstring
LC0:
    .ascii "Hello World\0"
    .text
.globl _main
_main:
LFB3:
    pushq   %rbp
LCFI0:
    movq    %rsp, %rbp
LCFI1:
    subq    $16, %rsp
LCFI2:
    movl    %edi, -4(%rbp)
    movq    %rsi, -16(%rbp)
    leaq    LC0(%rip), %rdi
    call    _puts
    movl    $0, %eax
    leave
    ret
LFE3:
    .section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support
EH_frame1:
    .set L$set$0,LECIE1-LSCIE1
    .long L$set$0
LSCIE1:
    .long   0x0
    .byte   0x1
    .ascii "zR\0"
    .byte   0x1
    .byte   0x78
    .byte   0x10
    .byte   0x1
    .byte   0x10
    .byte   0xc
    .byte   0x7
    .byte   0x8
    .byte   0x90
    .byte   0x1
    .align 3
LECIE1:
.globl _main.eh
_main.eh:
LSFDE1:
    .set L$set$1,LEFDE1-LASFDE1
    .long L$set$1
LASFDE1:
    .long   LASFDE1-EH_frame1
    .quad   LFB3-.
    .set L$set$2,LFE3-LFB3
    .quad L$set$2
    .byte   0x0
    .byte   0x4
    .set L$set$3,LCFI0-LFB3
    .long L$set$3
    .byte   0xe
    .byte   0x10
    .byte   0x86
    .byte   0x2
    .byte   0x4
    .set L$set$4,LCFI1-LCFI0
    .long L$set$4
    .byte   0xd
    .byte   0x6
    .align 3
LEFDE1:
    .subsections_via_symbols

내 다음 문제는이 출력을 어떻게 컴파일하고 GCC가 나를 위해 할 수 있습니까?


예, gcc를 사용하여 asm 코드를 컴파일 할 수 있습니다. 다음과 같이 컴파일하려면 -c를 사용하십시오.

gcc -c file.S -o file.o

그러면 file.o라는 개체 코드 파일이 제공됩니다. 링커를 호출하려면 위의 명령 후에 다음을 수행하십시오.

gcc file.o -o file

gcc어셈블리 파일을 입력으로 사용하고 필요에 따라 어셈블러를 호출 할 수 있습니다. 하지만 미묘한 점이 있습니다.

  • 파일 이름이 " .s"(소문자 's')로 끝나면 gcc어셈블러 호출합니다.
  • If the file name ends with ".S" (uppercase 'S'), then gcc applies the C preprocessor on the source file (i.e. it recognizes directives such as #if and replaces macros), and then calls the assembler on the result.

So, on a general basis, you want to do things like this:

gcc -S file.c -o file.s
gcc -c file.s

You can embed the assembly code in a normal C program. Here's a good introduction. Using the appropriate syntax, you can also tell GCC you want to interact with variables declared in C. The program below instructs gcc that:

  • eax shall be foo
  • ebx shall be bar
  • the value in eax shall be stored in foo after the assembly code executed

\n

int main(void)
{
        int foo = 10, bar = 15;
        __asm__ __volatile__("addl  %%ebx,%%eax"
                             :"=a"(foo)
                             :"a"(foo), "b"(bar)
                             );
        printf("foo+bar=%d\n", foo);
        return 0;
}

Yes, gcc can also compile assembly source code. Alternatively, you can invoke as, which is the assembler. (gcc is just a "driver" program that uses heuristics to call C compiler, C++ compiler, assembler, linker, etc..)


You can use GAS, which is gcc's backend assembler:

http://linux.die.net/man/1/as


If you have main.s file. you can generate object file by GCC and also as

# gcc -c main.s
# as main.s -o main.o

check this link, it will help you learn some binutils of GCC http://www.thegeekstuff.com/2017/01/gnu-binutils-commands/


nasm -f bin -o 2_hello 2_hello.asm

참고URL : https://stackoverflow.com/questions/7190050/how-do-i-compile-the-asm-generated-by-gcc

반응형