카테고리 없음

ARM 역 어셉블하는 방법(arm-linux-objdump)

achivenKakao 2007. 5. 16. 02:48
arm-linux-objdump 로 역어셈블을 해보자.

arm-linux-objdump -D test1.o > test1.s

다음과 같은 결과를 확인 할 수 있다.

hex로 덤프 떠서 보는 것보다 훨~~씬 편리하네..ㅎ


사용자 삽입 이미지

================================================================================================

[참고]

1. 개요

이 문서는 c로 작성된 프로그램을 역 어셈블 하는
것에 대하여 기술하였다.

2. 왜 역어셈블러를 해야 했을까?

이 문서는

arm용 gcc에서는 "arm-linux-gcc"에서 컴파일 하면

1) 스택주소가 증가 하는가 감소하는가

2) 어셈블러에서 c함수를 호출하기 위해서
매계 변수는 어떻게 넘겨야 하는 가

를 알아보기 위한 과정에서 발생되었다.

3. 역 어셈블 방법

역어셈블을 하기 위해서 필요한 실행화일은

arm-linux-gcc는 반드시 필요하며
arm-linux-objdump는 오브젝트 화일만 있을 경우에
필요하다.

먼저 예제 쏘스를 만들어 보자

test1.c

int testfunc( int a, int b )
{
int sum;

sum = a + b;
return sum;
}

우선 컴파일 해서 오브젝트 화일을 만들어 보자.

arm-linux-gcc -c test1.c

이 명령을 수행하면 test1.o 라는 화일이 발생된다.

우선 arm-linux-gcc를 이용하여 어셉블러 코드로
만드는 방법을 알아보자.

arm-linux-gcc -S test1.c

수행하면 test1.s라는 화일이 생성된다.

이 화일을 열어 보면

======= test1.s ================================================
@ Generated by gcc 2.95.2 19991024 (release) for ARM/elf
.file "test1.c"
gcc2_compiled.:
.text
.align 2
.global testfunc
.type testfunc,function
testfunc:
@ args = 0, pretend = 0, frame = 12
@ frame_needed = 1, current_function_anonymous_args = 0
mov ip, sp
stmfd sp!, {fp, ip, lr, pc}
sub fp, ip, #4
sub sp, sp, #12
str r0, [fp, #-16]
str r1, [fp, #-20]
ldr r3, [fp, #-16]
ldr r2, [fp, #-20]
add r3, r3, r2
str r3, [fp, #-24]
ldr r3, [fp, #-24]
mov r0, r3
b .L2
.L2:
ldmea fp, {fp, sp, pc}
.Lfe1:
.size testfunc,.Lfe1-testfunc
.ident "GCC: (GNU) 2.95.2 19991024 (release)"
======= test1.s ================================================

이렇게 나온다.

이번에는 arm-linux-objdump 로 역어셈블을 해보자.

arm-linux-objdump -D test1.o > test1.s

이렇게 수행 보자


======= test1.s ================================================
test1.o: file format elf32-littlearm

Disassembly of section .text:

00000000 <testfunc>:
0: e1a0c00d mov r12, sp
4: e92dd800 stmdb sp!, {r11, r12, lr, pc}
8: e24cb004 sub r11, r12, #4 ; 0x4
c: e24dd00c sub sp, sp, #12 ; 0xc
10: e50b0010 str r0, [r11, -#16]
14: e50b1014 str r1, [r11, -#20]
18: e51b3010 ldr r3, [r11, -#16]
1c: e51b2014 ldr r2, [r11, -#20]
20: e0833002 add r3, r3, r2
24: e50b3018 str r3, [r11, -#24]
28: e51b3018 ldr r3, [r11, -#24]
2c: e1a00003 mov r0, r3
30: ea00000b b 64 <testfunc+0x64>
34: e91ba800 ldmdb r11, {r11, sp, pc}
Disassembly of section .data:
======= test1.s ================================================


4. 마지막으로 아쉬운 점

내가 알기로는 쏘스코드가 포함된 어셈블러 코드 생성
방법이 있는 것으로 알고 있다.

예전에 한번 했던 기억이 나는데 기억이 영 나지 않아서
이곳에 적지 못했다.

혹시 여러분 중에 아는 분이 있다면 답장좀 달라.


8.20 How to get GCC to generate assembly code

Q: How can I peek at the assembly code generated by GCC?

Q: How can I create a file where I can see the C code and its assembly translation together?

A: Use the -S (note: capital S) switch to GCC, and it will emit the assembly code to a file with a .s extension. For example, the following command:

  gcc -O2 -S -c foo.c

will leave the generated assembly code on the file foo.s.

If you want to see the C code together with the assembly it was converted to, use a command line like this:

 gcc -c -g -Wa,-a,-ad [other GCC options] foo.c > foo.lst

which will output the combined C/assembly listing to the file foo.lst.

If you need to both get the assembly code and to compile/link the program, you can either give the -save-temps option to GCC (which will leave all the temporary files including the .s file in the current directory), or use the -Wa,aln=foo.s option which instructs the assembler to output the assembly translation of the C code (together with the hex machine code and some additional info) to the file named after the =.

출처 : http://www.delorie.com/djgpp/v2faq/faq8_20.html



5. 소유권

이 문서 지적 소유권은 (주)제이닷디엔티에 있다.
문서에 관련된 문의 사항이 있다면
frog@falinux.com으로 연락 바란다