-
C코드를 ll로 변환 후 Assembly ❤️바꾸는 방법(LLVM&Assembly코드는 M1pro arm64기계 기준임macOS)otool활용코딩_Coding_Dev책_정리/Embedded_Assembly_Low_Dev 2022. 8. 27. 04:38728x90
한글강의Assembly언어001_ld_as_C언어_Assembly언어로 변환 #assembly
https://youtu.be/9yt1Enp4EwU
Arm64 Assembly Cheat Sheet
잘 정리됨
https://modexp.wordpress.com/2018/10/30/arm64-assembly/
Rust❤️
Rust❤️러스트 .o 오브젝트(obj❤️) 파일 만드는 방법How create .obj file from Rust code -
https://economiceco.tistory.com/m/14900
내가 공부하려고 정리한 Github
https://github.com/YoungHaKim7/Arm64_Assembly_Language
LLVM -IR 대해
https://ji007.tistory.com/4
9분6초에 나옴
5. C to Assembly
자료 출처 MIT
https://youtu.be/wt7a5BOztuM
테스트 C코드
fib.c#include <stdio.h> #include <stdlib.h> int fib(int n) { int i, j; if (n < 2) return n; i = fib(n - 1); j = fib(n - 2); return (i + j); } int main() { int n = 40; printf("fib(%d) %d \n", n, fib(n)); return 0; }
clang 으로 LLVM IR Code 로 우선 변환clang -S -emit-llvm fib.c
fib.c 가 fib.ll 변환됨
fib.ll; ModuleID = 'fib.c' source_filename = "fib.c" target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128" target triple = "arm64-apple-macosx12.0.0" @.str = private unnamed_addr constant [13 x i8] c"fib(%d) %d \0A\00", align 1 ; Function Attrs: noinline nounwind optnone ssp uwtable define i32 @fib(i32 %0) #0 { %2 = alloca i32, align 4 %3 = alloca i32, align 4 %4 = alloca i32, align 4 %5 = alloca i32, align 4 store i32 %0, i32* %3, align 4 %6 = load i32, i32* %3, align 4 %7 = icmp slt i32 %6, 2 br i1 %7, label %8, label %10 8: ; preds = %1 %9 = load i32, i32* %3, align 4 store i32 %9, i32* %2, align 4 br label %20 10: ; preds = %1 %11 = load i32, i32* %3, align 4 %12 = sub nsw i32 %11, 1 %13 = call i32 @fib(i32 %12) store i32 %13, i32* %4, align 4 %14 = load i32, i32* %3, align 4 %15 = sub nsw i32 %14, 2 %16 = call i32 @fib(i32 %15) store i32 %16, i32* %5, align 4 %17 = load i32, i32* %4, align 4 %18 = load i32, i32* %5, align 4 %19 = add nsw i32 %17, %18 store i32 %19, i32* %2, align 4 br label %20 20: ; preds = %10, %8 %21 = load i32, i32* %2, align 4 ret i32 %21 } ; Function Attrs: noinline nounwind optnone ssp uwtable define i32 @main() #0 { %1 = alloca i32, align 4 %2 = alloca i32, align 4 store i32 0, i32* %1, align 4 store i32 40, i32* %2, align 4 %3 = load i32, i32* %2, align 4 %4 = load i32, i32* %2, align 4 %5 = call i32 @fib(i32 %4) %6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %5) ret i32 0 } declare i32 @printf(i8*, ...) #1 attributes #0 = { noinline nounwind optnone ssp uwtable "frame-pointer"="non-leaf" "min-legal-vector-width"="0" "no-trapping-math"="true" "probe-stack"="__chkstk_darwin" "stack-protector-buffer-size"="8" "target-cpu"="apple-m1" "target-features"="+aes,+crc,+crypto,+dotprod,+fp-armv8,+fp16fml,+fullfp16,+lse,+neon,+ras,+rcpc,+rdm,+sha2,+sha3,+sm4,+v8.5a,+zcm,+zcz" } attributes #1 = { "frame-pointer"="non-leaf" "no-trapping-math"="true" "probe-stack"="__chkstk_darwin" "stack-protector-buffer-size"="8" "target-cpu"="apple-m1" "target-features"="+aes,+crc,+crypto,+dotprod,+fp-armv8,+fp16fml,+fullfp16,+lse,+neon,+ras,+rcpc,+rdm,+sha2,+sha3,+sm4,+v8.5a,+zcm,+zcz" } !llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} !llvm.ident = !{!9} !0 = !{i32 2, !"SDK Version", [2 x i32] [i32 12, i32 3]} !1 = !{i32 1, !"wchar_size", i32 4} !2 = !{i32 1, !"branch-target-enforcement", i32 0} !3 = !{i32 1, !"sign-return-address", i32 0} !4 = !{i32 1, !"sign-return-address-all", i32 0} !5 = !{i32 1, !"sign-return-address-with-bkey", i32 0} !6 = !{i32 7, !"PIC Level", i32 2} !7 = !{i32 7, !"uwtable", i32 1} !8 = !{i32 7, !"frame-pointer", i32 1} !9 = !{!"Apple clang version 13.1.6 (clang-1316.0.21.2.5)"}
변환 된 fib.ll파일을 fib.s 어셈블리 파일로 다시 변환clang fib.ll -S
fib.s 코드.section __TEXT,__text,regular,pure_instructions .build_version macos, 12, 0 sdk_version 12, 3 .global _fib ; -- Begin function fib .p2align 2 _fib: ; @fib .cfi_startproc ; %bb.0: sub sp, sp, #32 stp x29, x30, [sp, #16] ; 16-byte Folded Spill add x29, sp, #16 .cfi_def_cfa w29, 16 .cfi_offset w30, -8 .cfi_offset w29, -16 str w0, [sp, #8] ldr w8, [sp, #8] subs w8, w8, #2 b.ge LBB0_2 b LBB0_1 LBB0_1: ldr w8, [sp, #8] stur w8, [x29, #-4] b LBB0_3 LBB0_2: ldr w8, [sp, #8] subs w0, w8, #1 bl _fib str w0, [sp, #4] ldr w8, [sp, #8] subs w0, w8, #2 bl _fib str w0, [sp] ldr w8, [sp, #4] ldr w9, [sp] add w8, w8, w9 stur w8, [x29, #-4] b LBB0_3 LBB0_3: ldur w0, [x29, #-4] ldp x29, x30, [sp, #16] ; 16-byte Folded Reload add sp, sp, #32 ret .cfi_endproc ; -- End function .global _main ; -- Begin function main .p2align 2 _main: ; @main .cfi_startproc ; %bb.0: sub sp, sp, #64 stp x29, x30, [sp, #48] ; 16-byte Folded Spill add x29, sp, #48 .cfi_def_cfa w29, 16 .cfi_offset w30, -8 .cfi_offset w29, -16 mov w8, #0 stur w8, [x29, #-12] ; 4-byte Folded Spill stur wzr, [x29, #-4] mov w8, #40 stur w8, [x29, #-8] ldur w9, [x29, #-8] ; implicit-def: $x8 mov x8, x9 str x8, [sp, #24] ; 8-byte Folded Spill ldur w0, [x29, #-8] bl _fib ldr x8, [sp, #24] ; 8-byte Folded Reload mov x10, x0 adrp x0, l_.str@PAGE add x0, x0, l_.str@PAGEOFF mov x9, sp str x8, [x9] ; implicit-def: $x8 mov x8, x10 str x8, [x9, #8] bl _printf ldur w0, [x29, #-12] ; 4-byte Folded Reload ldp x29, x30, [sp, #48] ; 16-byte Folded Reload add sp, sp, #64 ret .cfi_endproc ; -- End function .section __TEXT,__cstring,cstring_literals l_.str: ; @.str .asciz "fib(%d) %d \n" .subsections_via_symbols
// 링크로 연결해서 실행 파일 만들기 ld -macosx_version_min 12.0.0 -o fib fib.o -lSystem -syslibroot `xcrun -sdk macosx --show-sdk-path` -e _main -arch arm64 ls fib fib.c fib.ll fib.o fib.s ./fib fib(40) 102334155
gcc -o fib -Wall fib.c ls fib fib.c fib.ll fib.o fib.s // otool 을 이용해 Assembly 코드를 볼 수 있다. otool -tv fib
fib: (__TEXT,__text) section _fib: 0000000100003ec4 sub sp, sp, #0x20 0000000100003ec8 stp x29, x30, [sp, #0x10] 0000000100003ecc add x29, sp, #0x10 0000000100003ed0 str w0, [sp, #0x8] 0000000100003ed4 ldr w8, [sp, #0x8] 0000000100003ed8 subs w8, w8, #0x2 0000000100003edc b.ge 0x100003ef0 0000000100003ee0 b 0x100003ee4 0000000100003ee4 ldr w8, [sp, #0x8] 0000000100003ee8 stur w8, [x29, #-0x4] 0000000100003eec b 0x100003f24 0000000100003ef0 ldr w8, [sp, #0x8] 0000000100003ef4 subs w0, w8, #0x1 0000000100003ef8 bl _fib 0000000100003efc str w0, [sp, #0x4] 0000000100003f00 ldr w8, [sp, #0x8] 0000000100003f04 subs w0, w8, #0x2 0000000100003f08 bl _fib 0000000100003f0c str w0, [sp] 0000000100003f10 ldr w8, [sp, #0x4] 0000000100003f14 ldr w9, [sp] 0000000100003f18 add w8, w8, w9 0000000100003f1c stur w8, [x29, #-0x4] 0000000100003f20 b 0x100003f24 0000000100003f24 ldur w0, [x29, #-0x4] 0000000100003f28 ldp x29, x30, [sp, #0x10] 0000000100003f2c add sp, sp, #0x20 0000000100003f30 ret _main: 0000000100003f34 sub sp, sp, #0x40 0000000100003f38 stp x29, x30, [sp, #0x30] 0000000100003f3c add x29, sp, #0x30 0000000100003f40 mov w8, #0x0 0000000100003f44 stur w8, [x29, #-0xc] 0000000100003f48 stur wzr, [x29, #-0x4] 0000000100003f4c mov w8, #0x28 0000000100003f50 stur w8, [x29, #-0x8] 0000000100003f54 ldur w9, [x29, #-0x8] 0000000100003f58 mov x8, x9 0000000100003f5c str x8, [sp, #0x18] 0000000100003f60 ldur w0, [x29, #-0x8] 0000000100003f64 bl _fib 0000000100003f68 ldr x8, [sp, #0x18] 0000000100003f6c mov x10, x0 0000000100003f70 adrp x0, 0 ; 0x100003000 0000000100003f74 add x0, x0, #0xfa8 ; literal pool for: "fib(%d) %d \n" 0000000100003f78 mov x9, sp 0000000100003f7c str x8, [x9] 0000000100003f80 mov x8, x10 0000000100003f84 str x8, [x9, #0x8] 0000000100003f88 bl 0x100003f9c ; symbol stub for: _printf 0000000100003f8c ldur w0, [x29, #-0xc] 0000000100003f90 ldp x29, x30, [sp, #0x30] 0000000100003f94 add sp, sp, #0x40 0000000100003f98 ret
arm64 Assembly 해석하는 방법
https://github.com/Siguza/ios-resources/blob/master/bits/arm64.md
otool 활용법(macOS)
Comparing C to machine language
https://www.youtube.com/watch?v=yOyaJXpAYZQ
C언어 int_64 해결 힌트
https://stackoverflow.com/questions/31534474/format-lld-expects-type-long-long-int-but-argument-4-has-type-int64-t
반응형'코딩_Coding_Dev책_정리 > Embedded_Assembly_Low_Dev' 카테고리의 다른 글
Modern x64 Assembly 1: Beginning Assembly Programming (0) 2023.02.12 Assembly를 공부해야 하는 이유7분30초에 나옴 (2) 2023.01.26 eBook)게임처럼 쉽고 재미있게 배우는 어셈블리 언어 튜토리얼 [ PDF ] (0) 2023.01.22 eBook)The Art of Assembly Language, 2nd Edition (0) 2023.01.03 5. C to Assembly | MIT❤️ (0) 2022.08.27 Assembly Language & Computer Architecture | MIT❤️ (0) 2022.08.27 macOS M1 Assembly Language Tutorial❤️ (0) 2022.08.25 러스트WebAssembly라서 클릭했더니❤️ Assembly 강의하는 영상 ㅎㅎ 잠 안 올때 보면 잠 잘 올것 같다. ㅋㅋㅋ (0) 2022.06.21