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

(6)

#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), 60);
    mprotect((void *) code, 60, PROT_READ | PROT_WRITE | PROT_EXEC);
    code[ 0] = 0xa9800000 | 31 << 5 | ((-16/8)&127) << 15 | 30 | 19 << 10; // push(x30, x19);
    code[ 1] = 0xa9800000 | 31 << 5 | ((-16/8)&127) << 15 | 20 | 21 << 10; // push(x20, x21);
    code[ 2] = 0xf9400000 | 19 | 0 << 5 | 0 << 10;                         // x19 = [x0+0*8];
    code[ 3] = 0xd2800000 | 20 | 0x20 << 5;                                // x20 = 0x20;
    code[ 4] = 0xaa000000 | 31 << 16 | 0 | 20 << 5;                        // x0 = x20;
    code[ 5] = 0xd63f0000 | 19 << 5;                                       // BLR(x19);
    code[ 6] = 0x91000000 | 20 | 20 << 5 | 1 <<10;                         // x20 = x20 + 1;
    code[ 7] = 0xf1000000 | 31 | 20 << 5 | 0x7f << 10;                     // CMP(x20, 0x7f);
    code[ 8] = 0x54000000 | 0x01 | ((-4)&524287) << 5;                     // b.ne $-4
    code[ 9] = 0xd2800000 | 0 | 0xa << 5;                                  // x0 = 0xa;
    code[10] = 0xd63f0000 | 19 << 5;                                       // BLR(x19);
    code[11] = 0xa8c00000 | 31 << 5 | (+16/8)&127) << 15 | 20 | 21 << 10;  // pop(x20, x21);
    code[12] = 0xa8c00000 | 31 << 5 | (+16/8)&127) << 15 | 30 | 19 << 10;  // pop(x30, x19);
    code[13] = 0xd2800000 | 0 | 123 << 5;                                  // x0=123;
    code[14] = 0xd65f03c0;                                                 // ret;
    int i = ((int (*)(void **)) code)(vp);
    printf("i=%d\n", i);
    return 0;
}

(9)

こめんと欄


コメントお名前NameLink

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