0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AArch64のスタックポインタは16アラインしないといけないみたい

Posted at

筆者はTermuxでなんかを趣味としてやってる町工場の者である。

筆者はTermuxで、libc相当であるBionicのパッケージlibandroid-supportをインストールしてない状況下で、Zigコンパイラ(0.1も0.14も試した)がllegal InstructionとかBad System Callされるなと思いながらコンパイラ作ればよいじゃんとか思い9ccしようと。

筆者はAArch64で遊ぶに至る。

筆者の知識

AArch64の呼び出し規約、各命令ある程度とか、ある程度のシステムコール番号とか。

番地が一つ違ったら1バイト分のデータが入るんじゃね。こういう認識。

あと関数っぽいやつをやるにあたり関数が関数を呼び出す場合はfpとlrをいじる必要がある的な。アホみたいに暗記なう。

環境

$ as --version; ld --version
GNU assembler (GNU Binutils) 2.44
Copyright (C) 2025 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or later.                                This program has absolutely no warranty.                                          This assembler was configured for a target of `aarch64-linux-android'.
GNU ld (GNU Binutils) 2.44
Copyright (C) 2025 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.

ストアしてみる

次の、スタックポインタをアラインしてないところに格納してるアセンブリプログラムを作った。

00.s
.globl _start
_start:
mov w1,9
str w1,[sp,-7]!
mov x0,sp
mov x8,93
svc 0

32ビット整数を適当すぎるところに格納し、その後のスタックポインタを終了コードとするプログラム。

アセンブルしてリンクし、数回実行。

$ as -o 00.o 00.s
$ ld -o 00 00.o
$ ./00
57
$ ./00
233
$ ./00
121
$

このように、16を法としたら9に合同な値のみが得られた。

ストアする

次に、筆者は次のソースを書いた。

01.s
.globl _start
_start:
mov w1,9
str w1,[sp,-7]!
ldr w0,[sp],7
mov x8,93
svc 0

あるメモリにデータを格納した後にそのデータを取得することで固定の終了コード9を得られるのではという試みである。

アセンブルしリンクし実行すると、怒られる。

$ ./01; echo $?
Bus error
135

どうも変なところにスタックポインタをやってる状態でロードしたら怒られるっぽい。
あと後置変更せずロードもやったところ同様わず。

01b.s
.globl _start
_start:
mov w1,9
str w1,[sp,-7]!
ldr w0,[sp,7]
mov x8,93
svc 0
$ ./01b; echo $?
Bus error
135

なんでこの記事書いたの?

プログラム作ってるうちになんかミスったかなと

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?