ARM64の勉強#1
(0)
- 格安スマホや適当なAndroidタブレットに、Termuxをインストールして、clangを使っていろいろ実験しました。ソースコードはnanoで入力しています。
(1)
#include <stdio.h>
#include <stdint.h>
int main()
{
printf("%d\n", (int) sizeof (int32_t));
return 0;}
}
(2)
#include <stdio.h>
#include <stdint.h>
uint32_t t[] = { 0xd2800000 | 123 << 5, 0xd65f03c0 }; // x0=123; ret;
int main()
{
int (*fnc)() = (int (*)()) t;
int i = fnc();
printf("i=%d\n", i);
return 0;
}
- 実行したら、「Segmentation fault」になってしまいました。まあ当然か・・・。
(3)
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
int main()
{
uint32_t *code;
posix_memalign((void **) &code, sysconf(_SC_PAGESIZE), 8);
mprotect((void *) code, 8, PROT_READ | PROT_WRITE | PROT_EXEC);
code[0] = 0xd2800000 | 123 << 5; // x0=123;
code[1] = 0xd65f03c0; // ret;
int i = ((int (*)()) code)();
printf("i=%d\n", i);
return 0;
}
- 実行したら、「i=123」と表示されました。大成功です!
(4)
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
int main()
{
uint32_t *code;
posix_memalign((void **) &code, sysconf(_SC_PAGESIZE), 8);
mprotect((void *) code, 8, PROT_READ | PROT_WRITE | PROT_EXEC);
code[0] =
code[0] = 0xd2800000 | 123 << 5; // x0=123;
code[1] = 0xd65f03c0; // ret;
int i = ((int (*)()) code)();
printf("i=%d\n", i);
return 0;
}
(9)
- ここまでのまとめ
| MOVZ | 0xd2800000 + imm16 << 5 + reg | (2) | x0~x30にimm16を代入 |
| RET | 0xd65f03c0 | (2) | PC=x30 |
こめんと欄