ARM64の勉強#1

(0)

(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;
}

(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;
}

(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), 36);
    mprotect((void *) code, 36, PROT_READ | PROT_WRITE | PROT_EXEC);
    code[0] = 0xa9800000 | 30 | 19 << 10 | 31 << 5 | ((-16/8)&127) << 15; // x19, x30 をpush.
    code[1] = 0xaa000000 | 1 << 16 | 31 << 10 | 19; // x19 = x1; // ORR x19,xzr,x1 (MOV)
    code[2] = 0xd2800000 | 'A' << 5; // x0='A';
    code[3] = 0xd63f0000 | 19 << 5; // BLR x19;
    code[4] = 0xd2800000 | '\n' << 5; // x0='\n';
    code[5] = 0xd63f0000 | 19 << 5; // BLR x19;
    code[6] = 0xa8c00000 | 30 | 19 << 10 | 31 << 5 | ((+16/8)&127) << 15; // x19, x30 をpop.
    code[7] = 0xd2800000 | 123 << 5; // x0=123;
    code[8] = 0xd65f03c0; // ret;
    printf("%x\n", code[0]); // この行をなくすとセグメントフォールトするようになる。
    int i = ((int (*)(void *)) code)(putchar);
    printf("i=%d\n", i);
    return 0;
}

(5)

#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>

int main()
{
    uint32_t *code;
    void *vp[4]; vp[0] = putchar;
    posix_memalign((void **) &code, sysconf(_SC_PAGESIZE), 36);
    mprotect((void *) code, 36, PROT_READ | PROT_WRITE | PROT_EXEC);
    code[0] = 0xa9800000 | 30 | 19 << 10 | 31 << 5 | ((-16/8)&127) << 15; // x19, x30 をpush.
    code[1] = 0xf9400000 | 0 << 10 | 0 << 5 | 19; // x19 = [x0+0*8];
    code[2] = 0xd2800000 | 'A' << 5; // x0='A';
    code[3] = 0xd63f0000 | 19 << 5; // BLR x19;
    code[4] = 0xd2800000 | '\n' << 5; // x0='\n';
    code[5] = 0xd63f0000 | 19 << 5; // BLR x19;
    code[6] = 0xa8c00000 | 30 | 19 << 10 | 31 << 5 | ((+16/8)&127) << 15; // x19, x30 をpop.
    code[7] = 0xd2800000 | 123 << 5; // x0=123;
    code[8] = 0xd65f03c0; // ret;
    int i = ((int (*)(void **)) code)(vp);
    printf("i=%d\n", i);
    return 0;
}

(9)

こめんと欄


コメントお名前NameLink

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS