2013-05-15 44 views
0

下面代碼示例中的兩個奇怪的外觀沒有任何意義。我想知道linux內存對齊和佈局

  1. 爲什麼fooargv[0]之間的距離總是有什麼不同?
  2. 爲什麼不與4對齊?雖然預期結果是4的倍數,並且不是

我想知道奇怪外觀的詳細原因。

,我知道它不與ASLR

[[email protected] ~]# cat /etc/fedora-release 
Fedora release 14 (Laughlin) 
[[email protected] ~]# cat poc.c 
#include <stdio.h> 
int main(int argc, char *argv[]){ 
int foo; 
    printf("%d\n", (int)argv[0] - (int)&foo); 
} 
[[email protected] ~]# ./poc 
5345 
[[email protected] ~]# ./poc 
8465 
[[email protected] ~]# ./poc 
4641 
[[email protected] ~]# ./poc 
1201 
[[email protected] ~]# ./poc 
2881 
[[email protected] ~]# ./poc 
7073 
[[email protected] ~]# ./poc 
5905 
[[email protected] ~]# ./poc 
2225 
[[email protected] ~]# ./poc 
2465 
[[email protected] ~]# ./poc 
6017 
[[email protected] ~]# ./poc 
8657 
[[email protected] ~]# ./poc 
8401 
[[email protected] ~]# ./poc 
5073 
[[email protected] ~]# ./poc 
5505 
[[email protected] ~]# ./poc 
1761 
[[email protected] ~]# ./poc 
8609 
[[email protected] ~]# ./poc 
3665 
[[email protected] ~]# ./poc 
3633 
[[email protected] ~]# ./poc 
6257 
[[email protected] ~]# ./poc 
3441 
[[email protected] ~]# ./poc 
4961 
[[email protected] ~]# ./poc 
5233 
[[email protected] ~]# ./poc 
561 
[[email protected] ~]# ./poc 
3441 
[[email protected] ~]# ./poc 
2097 
[[email protected] ~]# ./poc 
1729 
[[email protected] ~]# ./poc 
1841 
[[email protected] ~]# ./poc 
2241 
[[email protected] ~]# ./poc 
2145 
[[email protected] ~]# ./poc 
6593 
[[email protected] ~]# ./poc 
5681 
[[email protected] ~]# ./poc 
737 
[[email protected] ~]# ./poc 
8353 
[[email protected] ~]# ./poc 
1937 
[[email protected] ~]# ./poc 
3937 
[[email protected] ~]# ./poc 
4769 
[[email protected] ~]# ./poc 
3441 
[[email protected] ~]# ./poc 
6097 
[[email protected] ~]# ./poc 
6673 
[[email protected] ~]# ./poc 
1857 
[[email protected] ~]# ./poc 
5617 
[[email protected] ~]# ./poc 
5473 
[[email protected] ~]# ./poc 
7313 
[[email protected] ~]# ./poc 
3921 
[[email protected] ~]# ./poc 
2369 
[[email protected] ~]# ./poc 
4609 
[[email protected] ~]# ./poc 
5569 
[[email protected] ~]# ./poc 
6209 
[[email protected] ~]# ./poc 
3457 
[[email protected] ~]# ./poc 
3665 
[[email protected] ~]# ./poc 
3297 
[[email protected] ~]# ./poc 
4465 
[[email protected] ~]# ./poc 
5281 
[[email protected] ~]# ./poc 
6017 
[[email protected] ~]# ./poc 
2705 
[[email protected] ~]# ./poc 
1601 
[[email protected] ~]# ./poc 
7457 
[[email protected] ~]# ./poc 
4145 
[[email protected] ~]# ./poc 
2353 
[[email protected] ~]# ./poc 
5537 
[[email protected] ~]# ./poc 
7873 
[[email protected] ~]# ./poc 
6449 
[[email protected] ~]# ./poc 
3297 

回答

0

1.涉及爲什麼FOO和argv之間的距離[0]是總是不同?

由於ASLR。

,我知道這是不是與ASLR涉及

你能肯定嗎?什麼是你的內核的虛擬地址空間隨機化參數值(randomize_va_space)?它應該是0.

2.爲什麼不與4對齊?雖然預期結果是4的倍數,並且不是

你想用什麼來證明?您的代碼計算從argv中的第一個字符串到本地變量foo的距離。這是什麼意思?

&如何處理參數和局部變量真的是OS和編譯器的依賴。看看下面的帖子:Where are C/C++ main functions parameters