免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4097 | 回复: 11
打印 上一主题 下一主题

[内存管理] c程序的内存布局问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-06-11 11:11 |只看该作者 |倒序浏览
ULK第20章有个例子,关于程序的内存布局的,例子如下:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>

  4. int main()
  5. {
  6.         char cmd[32];
  7.         brk((void *)0x8051000);
  8.         sprintf(cmd,"cat /proc/self/maps");
  9.         system(cmd);
  10.         return 0;
  11. }
复制代码
例子比较简单,编译执行。测试环境kernel 2.6.32-33-generic
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
#gcc -o memorylayout memorylayout.c
#ulimit -s unlimited; ./memorylayout
下面是执行结果:

使用的是经典布局,请问下面的栈为什么没有如书上所说从0xc0000000开始?
另外它堆的设置也没有起作用,如果说我堆的地址设置过小了,我把堆的截止地址放大了,也不行。

论坛徽章:
0
2 [报告]
发表于 2012-06-11 14:06 |只看该作者
我顶!没人遇到这个问题吗?

论坛徽章:
1
双鱼座
日期:2013-08-28 13:47:26
3 [报告]
发表于 2012-06-11 17:12 |只看该作者
ASLR(Address space layout randomization)

论坛徽章:
1
拜羊年徽章
日期:2015-03-03 16:15:43
4 [报告]
发表于 2012-06-12 18:25 |只看该作者
这里打印的是当前进程的memory layout, 不是linux system memory layout

论坛徽章:
0
5 [报告]
发表于 2012-06-12 20:02 |只看该作者
可以将环境变量 environ 相关的地址也打印出来,或可参考《嵌入式Linux内存管理.pdf》第14页有相关信息!

论坛徽章:
0
6 [报告]
发表于 2012-06-13 08:58 |只看该作者
回复 3# firkraag
能针对本例子解释下吗


   

论坛徽章:
0
7 [报告]
发表于 2012-06-13 08:59 |只看该作者
回复 4# linuxfellow
兄弟你的意思是?


   

论坛徽章:
0
8 [报告]
发表于 2012-06-13 09:29 |只看该作者
回复 5# skychgg
谢谢,讲的挺详细的


   

论坛徽章:
1
双鱼座
日期:2013-08-28 13:47:26
9 [报告]
发表于 2012-06-13 10:51 |只看该作者
本帖最后由 firkraag 于 2012-06-13 13:51 编辑

回复 6# sygspjlsj

I thought there was something wrong  in the code of the book. The reason is as linuxfellow have said,"It is not the memory layout of ./memorylayout, but the /bin/cat's memory layout."
You can modify the code like this:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main( ){
        char cmd[32];
        brk((void *)0x8051000);
        int pid=getpid();
        sprintf(cmd, "cat /proc/%d/maps",pid);
        system(cmd);
        return 0;
}

But before you run this program, you should shutdown the aslr first: "echo 0 > /proc/sys/kernel/randomize_va_space" as root user.

For what is the aslr, you can search it on the internet.

评分

参与人数 1可用积分 +2 收起 理由
瀚海书香 + 2 很给力!

查看全部评分

论坛徽章:
0
10 [报告]
发表于 2012-06-13 16:23 |只看该作者
回复 9# firkraag
谢谢,基本明白了其中的缘由!要好好的研究下proc文件系统


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP