2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PIE環境下でアセンブリからグローバル変数を扱う

Last updated at Posted at 2019-07-20

PIEにおいてはアドレスは相対的に指定する必要があります。
つまりいままでの mov rax, offset gvar といった絶対的な指定はできません。
こういったコードをアセンブルするには gcc に対して -no-pie オプションを渡す必要があります。

相対的な指定方法ですが実際にgccの出力を見るのが手っ取り早いです。
例えば以下のようなコードで

// main.c
int gvar = 1;

int main() {
    int *p = &gvar;
    return *p;
}

グローバル変数 gvar のアドレスをraxレジスタにロードする場合

AT&T記法ならgcc main.c -O0 -S

leaq    gvar(%rip), %rax

Intel記法ならgcc main.c -O0 -S -masm=intel

lea rax, gvar[rip]

つまり rip からの相対的な位置で指定します。
変数のラベルを指定するだけで具体的な値はアセンブラやリンカにおまかせできます。

gvar 変数は data セクションなどに確保されます。

	.data
gvar:
	.long	1
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?