{"id":201687,"date":"2020-11-25T07:12:18","date_gmt":"2020-11-24T23:12:18","guid":{"rendered":"http:\/\/4563.org\/?p=201687"},"modified":"2020-11-25T07:12:18","modified_gmt":"2020-11-24T23:12:18","slug":"%e5%85%b3%e4%ba%8e-linux-c-%e7%a8%8b%e5%ba%8f%e7%9a%84%e4%ba%94%e5%a4%a7%e5%86%85%e5%ad%98%e5%88%86%e5%8c%ba%e7%9a%84%e7%96%91%e9%97%ae","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=201687","title":{"rendered":"\u5173\u4e8e Linux c \u7a0b\u5e8f\u7684\u4e94\u5927\u5185\u5b58\u5206\u533a\u7684\u7591\u95ee"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5173\u4e8e Linux c \u7a0b\u5e8f\u7684\u4e94\u5927\u5185\u5b58\u5206\u533a\u7684\u7591\u95ee               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fffang <\/span>  <span><i><\/i> 13<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4e00\u4e2a\u5178\u578b\u7684 C \u7a0b\u5e8f\u7684\u5185\u5b58\u5e03\u5c40\u662f\u4ee5\u4e0b\u51e0\u4e2a\u5206\u533a(section)\u7ec4\u6210\uff1a<\/p>\n<ol>\n<li>\u4ee3\u7801\u6bb5(Text segment)<\/li>\n<li>\u6570\u636e\u6bb5(Initialized data segment)<\/li>\n<li>\u672a\u521d\u59cb\u5316\u7684\u6570\u636e\u6bb5(Uninitialized data segment)<\/li>\n<li>\u5806(Stack)<\/li>\n<li>\u6808(Heap)<\/li>\n<\/ol>\n<p>\u5bf9\u4e8e\u8fd9\u4e9b\u5206\u533a\u7684\u7406\u89e3\u662f\uff1a<\/p>\n<p>\u4ee3\u7801\u6bb5\u987e\u540d\u601d\u4e49\u50a8\u5b58\u7684\u662f\u4e8c\u8fdb\u5236\u7684\u53ef\u6267\u884c\u6307\u4ee4\u3002\u6570\u636e\u6bb5\u50a8\u5b58\u7684\u662f\u5df2\u521d\u59cb\u5316\u7684\u5185\u5bb9\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\uff0c\u6570\u5b57\u7b49\uff0cex.<code>char s [] =\u201c hello world\u201d;<\/code>\u3002\u672a\u521d\u59cb\u5316\u7684\u6570\u636e\u6bb5\uff0c\u58f0\u660e\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u4f46\u4e0d\u8d4b\u503c\uff0c\u5c06\u5b58\u50a8\u5728 bss \u6bb5\uff0c<code>static int i;<\/code>\u3002\u7a0b\u5e8f\u5458 malloc\/new \u7684\u533a\u57df\u4f1a\u5728\u5806\u4e0a\uff0c\u5806\u53ef\u4f38\u7f29\uff0c\u540c\u65f6\u4f1a\u4ea7\u751f\u5927\u91cf\u7684\u788e\u7247\u5185\u5b58\u3002\u6808\u7684\u7ed3\u6784\u662f LIFO \u7684\u7ed3\u6784\uff0c\u5b58\u50a8\u7740\u4e34\u65f6\u53d8\u91cf\uff0c\u6307\u9488\uff0c\u7531 OS \u8fdb\u884c\u7ba1\u7406\u3002\u6570\u636e\u6bb5\u548c\u4ee3\u7801\u6bb5\u662f\u5728\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\uff0c\u8fd0\u884c\u65f6\u7531 OS \u8bfb\u53d6\u5230\u5185\u5b58\u4e2d\u7684\u3002BSS \u6bb5\u4e0d\u5728\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\uff0c\u53ef\u6267\u884c\u6587\u4ef6\u4e2d BSS \u6bb5\u4ec5\u4ec5\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u6807\u8bb0\u6240\u6709\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u5360\u7528\u7a7a\u95f4\u7684\u603b\u548c\u3002\u5728\u8bfb\u53d6\u53ef\u6267\u884c\u6587\u4ef6\u65f6\uff0cOS \u5728\u6570\u636e\u6bb5\u65c1\u8fb9\uff0c\u5f00\u8f9f\u8fd9\u4e9b\u533a\u57df\u5e76\u5c06\u5b83\u4eec\u8d4b\u503c\u4e3a 0 \u3002<\/p>\n<p>\u8fd9\u662f\u6211\u7684\u7406\u89e3\uff0c\u6709\u9519\u8bef\u7684\u5730\u65b9\u8fd8\u8bf7\u6307\u6559\u3002\u4ee5\u4e0b\u662f\u6211\u7684\u95ee\u9898\uff1a<\/p>\n<p>\u5982\u679c\u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u53d8\u91cf\u5b9a\u4e49\u5982\uff0c<code>static int i;<\/code>\uff0c\u7136\u540e\u7528 malloc \u8fdb\u884c\u5206\u914d\uff0c\u8fd9\u5757\u5185\u5b58\u4f4d\u4e8e\u4f55\u5904\u5462\uff1f\u8fd8\u6709\u5982<code>char s [];<\/code>\uff0c\u4ece\u786c\u76d8\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5230\u5185\u5b58\uff0c\u5e94\u8be5\u4f1a\u5728\u5806\u533a\uff0c\u7136\u540e\u8d4b\u503c\u7ed9<code>s<\/code>\uff0c\u8fd9\u5757\u5185\u5b58\u4f4d\u4e8e\u4f55\u5904\u5462\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>9<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4108541\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : codehz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1. \u5168\u5c40\u53d8\u91cf\u4e0d\u662f\u4ece malloc \u83b7\u53d6\u7684\uff0cmalloc \u62ff\u5230\u7684\u662f\u65b0\u7684\u6307\u9488\u3002\u3002\u3002<br \/>2. char s[]; \u662f\u975e\u6cd5\u7684\u53d8\u91cf\u58f0\u660e\uff0c\u76f4\u63a5\u7f16\u8bd1\u4e0d\u8fc7                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4108542\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chinuno <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1.\u9759\u6001\u53d8\u91cf\u7f16\u8bd1\u597d\u4f4d\u7f6e\u5c31\u662f\u5728 bss \u91cc\uff0cmalloc \u8981\u600e\u4e48\u7ed9\u4ed6\u5206\u914d\uff1f malloc \u51fa\u6765\u7684\u662f\u4e2a\u6307\u9488\uff0c\u9876\u591a\u53d6\u503c\u7ed9\u4ed6\u8d4b\u503c<br \/>2.char s[];\u4f60\u8bd5\u8bd5\u80fd\u4e0d\u80fd\u7f16\u8bd1\u3002\u540e\u9762\u5e26\u5b57\u7b26\u4e32\u7684\u60c5\u51b5\u4e0b\u624d\u53ef\u4ee5\u7701\u7565\u957f\u5ea6\uff0c\u56e0\u4e3a\u9759\u6001\u5b57\u7b26\u4e32\u6570\u636e\u4ee5\u53ca\u56fa\u5b9a\u4e86\uff0c\u7f16\u8bd1\u7684\u65f6\u5019\u786e\u5b9a\u4e0b\u6765\u4e86\u3002\u5047\u8bbe\u4f60\u5236\u5b9a\u4e86 s \u7684\u957f\u5ea6\u7279\u522b\u957f\u591f\u4f60\u7528\u4e86\uff0c\u90a3\u4f60\u53ea\u80fd\u628a\u5806\u91cc\u7684\u6587\u4ef6\u6570\u636e\u590d\u5236\u5230 s \u672c\u8eab\u7684\u5185\u5b58\u7a7a\u95f4\uff08 bss \u6bb5\uff09\u5185                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4108543\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : codehz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             3. \u786c\u76d8\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5230\u5185\u5b58\u7684\u8fc7\u7a0b\uff0c\u4e0d\u4f1a\u5e2e\u4f60\u5206\u914d\u5185\u5b58\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f20\u9012\u4e00\u4e2a\u591f\u5927\u7684\u5168\u5c40\u53d8\u91cf\u6570\u7ec4\u3002\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4108544\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : fffang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @chinuno <br \/>@codehz                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4108545\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : fffang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @chinuno <br \/>@codehz <br \/>\u60f3\u4e86\u4e0b\u5e94\u8be5\u8fd9\u4e48\u63cf\u8ff0\uff1a\u5b9a\u4e49\u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u90a3\u5b83\u663e\u7136\u5e94\u8be5\u5728 BSS \u6bb5\u5185\uff0c\u4f46\u662f\u957f\u5ea6\u4e0d\u786e\u5b9a\uff0c\u5728\u8fd0\u884c\u65f6\u901a\u8fc7\u624d\u80fd\u77e5\u9053\u5b83\u7684\u957f\u5ea6\uff0c\u8fd9\u65f6\u4f1a\u5982\u4f55\u5206\u914d BSS \u6bb5\u5462\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4108546\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : XiaoxiaoPu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @fffang \u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u65f6\u5c31\u786e\u5b9a\u4e86\u957f\u5ea6\uff0c\u4e0d\u53ef\u80fd\u8fd0\u884c\u65f6\u624d\u786e\u5b9a\u957f\u5ea6                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4108547\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : codehz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @fffang \u4e0d\u80fd\u505a\u5230\uff0cbss \u6bb5\u7684\u4e1c\u897f\u957f\u5ea6\u5fc5\u987b\u662f\u7f16\u8bd1\u7684\u65f6\u5019\u5df2\u77e5\u7684\uff0c\u6ca1\u6709\u4efb\u4f55\u4f8b\u5916\u3002\u3002\u3002<br \/>\u4f46\u662f\u8fd9\u4e0d\u59a8\u788d\u4f60\u653e\u4e00\u4e2a\u6307\u9488\uff0c\u6307\u9488\u7684\u5927\u5c0f\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u6307\u5411\u7684\u5185\u5bb9\u53ef\u4ee5\u4e0d\u786e\u5b9a\u5927\u5c0f\uff08\u4e5f\u4e0d\u786e\u5b9a\u662f\u54ea\u4e2a\u5185\u5b58\u533a\u57df\uff0c\u6216\u8005\u65e0\u6548\uff09<br \/>\u8fd9\u65f6\u5019\u5c31\u53ef\u4ee5 malloc \u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4108548\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : raaaaaar <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6709\u4e66\u4e13\u95e8\u8bb2\u8fd9\u4e9b\u4e1c\u897f\u4e48\uff0c\u8981\u770b\u5185\u5b58\u7ba1\u7406\u8fd8\u662f\u5565\uff0c\u6211\u4ee5\u524d\u4e5f\u770b\u8fc7\u8fd9\u4e2a\uff0c\u4e0d\u8fc7\u4e0d\u662f\u5f88\u900f\u5f7b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4108549\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sockpuppet9527 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1. malloc \u5206\u51fa\u6765\u7684\u4e1c\u897f\u662f\u903b\u8f91\u5730\u5740\uff0c\u7ed9\u5230\u4f60\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6307\u5411\u8be5\u903b\u8f91\u5730\u5740\u7684\u6307\u9488\uff0c\u5177\u4f53\u786c\u4ef6\u4e0a\u7684\u5730\u533a\u8981\u7ecf\u8fc7 MMU \u3002<br \/>2. \u786c\u76d8\u8bfb\u53d6\u7684\u5185\u5bb9\u4f60\u53ef\u4ee5\u653e\u5230\u5806\uff0c\u4e5f\u53ef\u4ee5\u653e\u5230\u6808\u3002\u6808\u662f\u8981\u5148\u58f0\u660e\u957f\u5ea6\uff0c\u6bd4\u5982 char s[100]; \uff0c\u8fd9\u5c31\u662f\u6808\u548c\u5806\u6700\u5927\u7684\u4e0d\u540c\uff0c\u6808\u7684\u6df1\u6d45\u662f\u56fa\u5b9a\u7684\uff0c\u5806\u662f\u52a8\u6001\u7684\uff0c\u5c31\u7b97\u5230\u4e86\u6c47\u7f16\u5c42\u9762\uff0c\u4f60\u5199\u4e00\u4e2a\u65b9\u6cd5\uff0c\u6808\u91cc\u9762\u53d8\u91cf\u7684\u4f7f\u7528\u4e5f\u9700\u8981\u5148\u58f0\u660e\u957f\u5ea6\uff0c\u8d8a\u4e86\u6808\u5c31\u8fdd\u6cd5\u4e86\u3002<\/p>\n<p>\u53e6\u5916\u5173\u4e8e\u5185\u5b58(\u903b\u8f91\u5730\u5740)\u5728\u4f55\u5904\uff0c\u8fd9\u4e2a\u662f\u52a8\u6001\u7684,\u53c2\u8003\u4e00\u672c\u5185\u6838\u4e66\u4e0a\u7684 hello world \u4f8b\u5b50\uff1a<\/p>\n<p>&#8220;`<br \/>08048368 &lt;greeting&gt;:<br \/>8048368: 55 push %ebp<br \/>8048369: 89 e5 mov %esp,%ebp<br \/>804836b: 83 ec 08 sub $0x8,%esp<br \/>804836e: 83 ec 0c sub $0xc,%esp<br \/>8048371: 68 84 84 04 08 push $0x8048484<br \/>8048376: e8 35 ff ff ff call 80482b0 &lt;[email&#160;protected]&gt;<br \/>804837b: 83 c4 10 add $0x10,%esp<br \/>804837e: c9 leave<\/p>\n<p>804837f: c3 ret<br \/>08048380 &lt;main&gt;:<br \/>8048380: 55 push %ebp<br \/>8048381: 89 e5 mov %esp,%ebp<br \/>8048383: 83 ec 08 sub $0x8,%esp<br \/>8048386: 83 e4 f0 and $0xfffffff0,%esp<br \/>8048389: b8 00 00 00 00 mov $0x0,%eax<br \/>804838e: 83 c0 0f add $0xf,%eax<br \/>8048391: 83 c0 0f add $0xf,%eax<br \/>8048394: c1 e8 04 shr $0x4,%eax<br \/>8048397: c1 e0 04 shl $0x4,%eax<br \/>804839a: 29 c4 sub %eax,%esp<br \/>804839c: e8 c7 ff ff ff call 8048368 &lt;greeting&gt;<br \/>80483a1: c9 leave<br \/>80483a2: c3 ret<br \/>80483a3: 90 nop<br \/>&#8220;`<\/p>\n<p>\u4f60\u53ef\u4ee5\u770b\u5230 main \u7684\u5165\u53e3\u662f 0x8048380\uff0c\u8fd9\u5176\u5b9e\u662f\u7ed9\u5230 CPU \u7684 EIP \u5bc4\u5b58\u5668\u4f7f\u7528\u7684\uff0c\u6307\u5411\u7684\u865a\u62df\u5730\u5740\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e Linux c \u7a0b\u5e8f\u7684\u4e94\u5927\u5185&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/201687"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=201687"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/201687\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=201687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=201687"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=201687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}