{"id":1157,"date":"2014-09-06T04:16:48","date_gmt":"2014-09-06T04:16:48","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1157"},"modified":"2014-09-06T04:16:48","modified_gmt":"2014-09-06T04:16:48","slug":"%e8%bd%ac%e5%9c%a8linux%e4%b8%8b%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b%e5%8f%af%e6%89%a7%e8%a1%8c%e7%9a%84%e4%ba%8c%e8%bf%9b%e5%88%b6%e6%96%87%e4%bb%b6bak","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1157.html","title":{"rendered":"[collect]\u5728Linux\u4e0b\u5982\u4f55\u67e5\u770b\u53ef\u6267\u884c\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6[bak]"},"content":{"rendered":"<p>\u9898\u76ee\u56e0\u4e3a\u4e0d\u77e5\u9053\u600e\u4e48\u53d6\u597d\uff0c\u6240\u4ee5\u5c31\u7528\u4e86\u4e2a\u53e3\u8bed\u5316\u7684\u5185\u5bb9\u5927\u81f4\u8868\u8fbe\u51fa\u610f\u601d\u5c31\u884c\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u4e00\u4e9b\u547d\u4ee4\u5728\u4e4b\u524d\u7684\u6587\u7ae0\u4e2d\u4e5f\u6709\u6d89\u53ca\uff0c\u611f\u5174\u8da3\u7684\u8bdd\u53ef\u4ee5\u81ea\u5df1\u5c1d\u8bd5\u4e00\u4e0b\uff0c\u4e0b\u9762\u5f00\u59cb\u6587\u7ae0\u7684\u4e3b\u4f53\u5185\u5bb9\uff1a<\/p>\n<hr \/>\n<p>Linux \u4e0b\u6709\u4e00\u7cfb\u5217\u7684\u5de5\u5177\u53ef\u4ee5\u8ba9\u4f60\u5728\u4e0d\u6267\u884c\u4e00\u4e2a\u7a0b\u5e8f\u7684\u60c5\u51b5\u4e0b\u67e5\u770b\u5176\u5404\u79cd\u4fe1\u606f\uff0c\u5982\uff1a\u4f9d\u8d56\u4e8e\u4ec0\u4e48\u5e93\uff0c\u662f32\u4f4d\u7684\u8fd8\u662f64\u4f4d\u7684\u2026\u2026<\/p>\n<h5>\u76f8\u5173\u57fa\u7840\u77e5\u8bc6<\/h5>\n<p>\u5728\u5f00\u59cb\u4e4b\u524d\u6211\u4eec\u9700\u8981\u4e86\u89e3\u7684\u662f\u4e00\u4e9b\u76f8\u5173\u7684\u77e5\u8bc6\u3002\u867d\u7136\u8bf4\u5f88\u57fa\u7840\u4f46\u662f\u6211\u5176\u5b9e\u4e5f\u662f\u771f\u7684\u8981\u7528\u5230\u7684\u65f6\u5019\u624d\u53bb\u770b\u4e86\u4e0b\u3002<\/p>\n<p>\u9996\u5148\u662f\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u683c\u5f0f\u3002\u663e\u7136\u7684\u53ef\u6267\u884c\u6587\u4ef6\u5fc5\u987b\u6ee1\u8db3\u67d0\u79cd\u683c\u5f0f\u624d\u80fd\u771f\u7684\u88ab\u7cfb\u7edf\u8bfb\u53d6\u5e76\u6267\u884c\u3002\u76ee\u524d\u5e38\u89c1 *nix \u7cfb\u7edf\u90fd\u662f\u4f7f\u7528\u7684 <a title=\"Executable and Linkable Format\" href=\"http:\/\/en.wikipedia.org\/wiki\/Executable_and_Linkable_Format\" target=\"_blank\" rel=\"nofollow\">ELF<\/a> \u683c\u5f0f\u3002Windows \u4f7f\u7528\u7684\u662f<a title=\"Portable Executable\" href=\"http:\/\/en.wikipedia.org\/wiki\/Portable_Executable\" target=\"_blank\" rel=\"nofollow\">PE(Portable Executable)<\/a> \u683c\u5f0f\uff0c\u662f\u5728\u53e6\u4e00\u4e2a\u53e4\u8001\u7684\u683c\u5f0f <a title=\"Shortest ELF for \u201cHello worldn\u201d?\" href=\"http:\/\/codegolf.stackexchange.com\/a\/5726\" target=\"_blank\" rel=\"nofollow\">COFF<\/a> \u4e0a\u6539\u8fdb\u800c\u6765\u7684\u3002<\/p>\n<p>\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5bf9\u4e8e<a title=\"Shortest ELF for \u201cHello worldn\u201d?\" href=\"http:\/\/codegolf.stackexchange.com\/a\/5726\" target=\"_blank\" rel=\"nofollow\">\u5185\u5bb9\u5982\u4e0b\u7684\u4e00\u4e2a\u6587\u4ef6<\/a>\uff1a<\/p>\n<pre><code>00000000  7f 45 4c 46 01 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|\n00000010  02 00 03 00 01 00 00 00  54 80 04 08 34 00 00 00  |........T...4...|\n00000020  00 00 00 00 00 00 00 00  34 00 20 00 01 00 00 00  |........4. .....|\n00000030  00 00 00 00 01 00 00 00  00 00 00 00 00 80 04 08  |................|\n00000040  00 80 04 08 6f 00 00 00  6f 00 00 00 05 00 00 00  |....o...o.......|\n00000050  00 10 00 00 b0 04 b2 0c  b9 63 80 04 08 cd 80 b0  |.........c......|\n00000060  01 cd 80 48 65 6c 6c 6f  20 77 6f 72 6c 64 0a     |...Hello world.|<\/code><\/pre>\n<p>\u5c31\u662f\u4e00\u4e2a\u5408\u6cd5\u7684 ELF \u683c\u5f0f\u6587\u4ef6\u3002\u5728 Linux \u4e0b\u6b64\u6587\u4ef6\u5982\u679c\u88ab\u8bbe\u7f6e\u4e3a\u53ef\u6267\u884c\u7684\u8bdd\uff0c\u901a\u8fc7 Shell \u6267\u884c\u65f6 Linux \u5c31\u4f1a\u5148\u6839\u636e\u524d\u9762\u7684 <code>.ELF<\/code> \u5934\u6765\u5224\u65ad\u8fd9\u662f\u4e00\u4e2a ELF \u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528\u518d\u53c8 Loader \u9a8c\u8bc1\u5e76\u8f7d\u5165\u5185\u5b58\u6700\u540e\u5f00\u59cb\u6267\u884c\u3002\u672c\u6587\u540e\u9762\u5927\u90e8\u5206\u5185\u5bb9\u90fd\u662f\u9488\u5bf9 ELF \u683c\u5f0f\u6765\u8bb2\u7684\u3002<\/p>\n<p>\u5728\u4e4b\u540e\u662f\u5173\u4e8e Linker \u7684\u4e1c\u897f\u3002\u7f51\u4e0a\u6709\u4e00\u4efd <code>gold<\/code> \u4f5c\u8005\u5199\u7684<a title=\"A ToC of the 20 part linker essay\" href=\"http:\/\/lwn.net\/Articles\/276782\/\" target=\"_blank\" rel=\"nofollow\">\u4e00\u7cfb\u5217\u6587\u7ae0<\/a>\u975e\u5e38\u8be6\u7ec6\u7684\u4ecb\u7ecd\u4e86 Linker \u8fd9\u4e2a\u719f\u6089\u800c\u53c8\u964c\u751f\u7684\u4e1c\u897f\u5230\u5e95\u505a\u4e86\u4e9b\u4ec0\u4e48\u3002\u8bf4\u5b9e\u8bdd\u5176\u5185\u5bb9\u6709\u70b9\u592a\u8fc7\u9ad8\u6df1\uff0c\u770b\u8fc7\u4ee5\u540e\u6211\u5c31\u8bb0\u5f97\u8fd9\u51e0\u4e2a\u4e1c\u897f\u7684\u4e1c\u897f\uff1a<\/p>\n<ul>\n<li><a title=\"soname on Wikipedia\" href=\"http:\/\/en.wikipedia.org\/wiki\/Soname\" target=\"_blank\" rel=\"nofollow\">soname<\/a>, \u4e5f\u5c31\u662f\u52a8\u6001\u94fe\u63a5\u5e93 (&#8220;libfoo.so&#8221; \u8fd9\u7c7b) \u7684\u540d\u5b57\u548c\u5b83\u76f8\u5173\u7684\u51e0\u4e2a\u4e1c\u897f\u3002\u6bd4\u5982\u4f60\u7f16\u8bd1\u7684\u65f6\u5019\u7528\u662f\u5199\u7684 <code>gcc bar.c -lfoo -o bar<\/code>\uff0c\u90a3\u4e48\u4f60 link \u7684\u662f <code>libfoo<\/code>\uff0c\u5bf9\u5e94\u7684 <strong>link name<\/strong> \u662f <code>libfoo.so<\/code>\u3002\u4f46\u4e8b\u5b9e\u4e0a\u8bb0\u5f55\u5230\u4f60\u7f16\u8bd1\u51fa\u6765\u7684 <code>bar<\/code> \u6587\u4ef6\u4e2d\u7684\u53ef\u80fd\u662f <code>libfoo.so.2<\/code>\uff0c\u5bf9\u5e94\u4e86\u4e00\u4e2a\u5177\u4f53\u7684\u7248\u672c\uff0c\u8fd9\u4e2a\u540d\u5b57\u5c31\u662f <strong>soname<\/strong>\u3002\u800c\u4e8b\u5b9e\u4e0a\u5f53\u7a0b\u5e8f\u8fd0\u884c\u8d77\u6765\u7684\u65f6\u5019\u5b83\u627e\u5230\u7684\u52a8\u6001\u94fe\u63a5\u5e93\u6587\u4ef6\u53ef\u80fd\u662f <code>libfoo.bar.2.1<\/code>\uff0c\u8fd9\u4e2a\u88ab\u79f0\u4f5c <strong>fully qualified name<\/strong>\u3002\u5176\u4e2d <code>soname<\/code> \u4e2d\u7684\u7248\u672c\u53f7\u5bf9\u5e94\u7740\u76f8\u4e92\u53ef\u4ee5\u4e0d\u517c\u5bb9\u7684 <code>libfoo<\/code> \u7684\u4e0d\u540c\u7248\u672c\uff0c\u800c <strong>fully qualified name<\/strong> \u4e2d\u6700\u540e\u4e00\u4e2a\u5c0f\u7248\u672c\u53f7\u8bb0\u5f55\u7740\u8fd9\u662f\u7b2c\u51e0\u4e2a &#8220;fix&#8221;\u3002<\/li>\n<li><a title=\"Beautiful Native Libraries\" href=\"http:\/\/lucumr.pocoo.org\/2013\/8\/18\/beautiful-native-libraries\/#which-language\" target=\"_blank\" rel=\"nofollow\">RPATH \u548c RUNPATH<\/a> \u662f ELF \u6587\u4ef6\u4e2d\u8bb0\u5f55 \u201c\u5230\u54ea\u91cc\u53bb\u627e\u5404\u79cd .so \u6587\u4ef6\u201d \u7684\u4e1c\u897f\u3002\u4f60\u77e5\u9053 Windows \u4e0a\u7684 dll \u6587\u4ef6\u4f1a\u73b0\u5728\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e0b\u627e\uff0c\u7136\u540e\u5728 <code>PATH<\/code> \u4e0a\u4e00\u4e2a\u4e2a\u627e\u3002\u5728 Linux \u4e0b\u8fd0\u884c\u65f6\u67e5\u627e .so \u6587\u4ef6\u7684\u65b9\u6cd5\u9664\u4e86\u6839\u636e\u7cfb\u7edf\u4e2d\u7684\u4e00\u4e9b\u9884\u8bbe\u503c\u548c\u89c4\u5219\uff0c\u8fd8\u4f1a\u6cbf\u7740 <code>RPATH<\/code> \u548c <code>RUNPATH<\/code> \u4e00\u4e2a\u4e2a\u627e\u3002\u4e24\u8005\u4e4b\u95f4\u7684\u533a\u522b\u8c8c\u4f3c\u662f\u5728\u67e5\u627e\u7684\u4f18\u5148\u7ea7\u4e0a\u3002<\/li>\n<li>\u52a8\u6001\u94fe\u63a5\u8981\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u505a\u5404\u79cd\u795e\u5947\u7684\u4e8b\u60c5\uff0c\u90a3\u4e48\u5b8c\u6210\u8fd9\u4e9b\u4e1c\u897f\u7684\u4ee3\u7801\u5176\u5b9e\u90fd\u662f\u5728 <code>ld.so<\/code> \u91cc\u9762\u3002\u52a8\u6001\u94fe\u63a5\u7684\u7a0b\u5e8f\u5e76\u4e0d\u662f\u8fd9\u771f\u6b63\u5b8c\u6574\u7684\uff0c\u5728\u8fd0\u884c\u65f6\u9700\u8981 <code>ld.so<\/code> \u6765\u505a\u5f88\u591a\u4e8b\u60c5\u3002\u4e00\u4e2a\u795e\u5947\u7684\u4f8b\u5b50\u662f\u4f60\u7684\u7a0b\u5e8f\u4e2d\u9996\u6b21\u8c03\u7528\u4efb\u4f55\u4e00\u4e2a\u5916\u90e8\u52a8\u6001\u5e93\u4e2d\u7684\u51fd\u6570\u7684\u65f6\u5019\u90fd\u4f1a\u6709\u989d\u5916\u5f00\u9500\uff0c<code>ld.so<\/code> \u4f1a\u901a\u8fc7 ELF \u6587\u4ef6\u4e2d\u7684\u4fe1\u606f\u6765\u627e\u5230\u8fd9\u4e2a\u51fd\u6570\u7684\u771f\u6b63\u5185\u5b58\u5730\u5740\u5e76\u628a\u5b83\u8bb0\u5f55\u5728\u53eb\u505a Procedure Linkage Table(PLT) \u7684\u8868\u4e2d\uff0c\u4f7f\u5f97\u540e\u7eed\u7684\u8c03\u7528\u53ef\u4ee5\u7701\u53bb\u8fd9\u4e00\u6b65\u3002Linux \u4e0b\u53ef\u4ee5\u7528 <code>man ld.so<\/code> \u67e5\u770b\u76f8\u5173\u6587\u6863\u3002<\/li>\n<\/ul>\n<h5>\u51c6\u5907<\/h5>\n<p>\u6211\u5728\u81ea\u5df1\u7684\u673a\u5668\u4e0a\u7f16\u8bd1\u4e00\u4e2a\u7b80\u5355\u7684 C \u6587\u4ef6\uff1a<\/p>\n<pre><code>$ cat bar.c\n#include &lt;stdio.h&gt;\n\nvoid foo() {\n    puts(\"hello world.\");\n}\n\nint main() {\n    foo();\n    return 0;\n}\n$ gcc -g bar.c -o bar\n$ .\/bar\nhello world.<\/code><\/pre>\n<p>\u540e\u6587\u4f1a\u7528\u5230\u4ea7\u751f\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6 <code>bar<\/code> \u4f5c\u4e3a\u4f8b\u5b50\u3002<\/p>\n<h5>\u57fa\u7840\u4fe1\u606f<\/h5>\n<p>\u4e00\u4e0a\u6765\u5f53\u7136\u5e94\u8be5\u7528\u4e07\u80fd\u7684 <span style=\"color: #ff0000;\"><strong><code>file\u547d\u4ee4<\/code><\/strong><\/span> \u6765\u770b\u770b\u5b83\u662f\u5565\uff1a<\/p>\n<pre><code>$ file bar\nbar: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU\/Linux 2.6.32, BuildID[sha1]=0xd677a3aba13e5b248c20e0d96c5d9328ae66231d, not stripped<\/code><\/pre>\n<p><span style=\"color: #ff0000;\"><strong><code>file<\/code> \u7684\u5de5\u4f5c\u539f\u7406\u662f\u6839\u636e\u7cfb\u7edf\u4e2d\u7684<\/strong> <code>magic<\/code> \u6587\u4ef6\uff0c\u8bfb\u53d6 <code>bar<\/code> \u5f00\u5934\u7684\u90e8\u5206\u6765\u5224\u5b9a\u6587\u4ef6\u7684\u7c7b\u578b\u3002<\/span>\u4e4b\u540e\u5bf9\u4e8e\u67d0\u4e9b\u6587\u4ef6\u7c7b\u578b\u53ef\u4ee5\u7ee7\u7eed\u7684\u8bfb\u53d6\u76f8\u5173\u4fe1\u606f\u5e76\u89e3\u6790\u51fa\u6765\u3002\u8fd9\u91cc <code>file<\/code> \u8ba4\u51fa\u4e86 <code>bar<\/code> \u662f\u4e00\u4e2a ELF \u683c\u5f0f\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5e76\u4ece ELF \u5934\u6765\u8bfb\u51fa\u4e86\u5173\u4e8e\u5927\u5c0f\u7aef\uff0c32\/64\u67b6\u6784\uff0c\u662f\u5426\u52a8\u6001\u94fe\u63a5\u7b49\u7b49\u4fe1\u606f\u3002\u672b\u5c3e\u7684 <code>not stripped<\/code> \u6307\u7684\u662f\u6587\u4ef6\u5305\u542b\u8c03\u8bd5\u4fe1\u606f\u3002<\/p>\n<p>\u8981\u770b\u5230\u50cf\u6587\u7ae0\u5f00\u5934\u90a3\u6837\u9ad8\u7aef\u7684\u5341\u516d\u8fdb\u5236\u52a0 ASCII \u7801\u8868\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code><span style=\"color: #ff0000;\"><strong>xxd\u547d\u4ee4<\/strong><\/span>\uff1a<\/code><\/p>\n<pre><code>$ xxd bar | head -4\n0000000: 7f45 4c46 0201 0100 0000 0000 0000 0000  .ELF............\n0000010: 0200 3e00 0100 0000 3004 4000 0000 0000  ..&gt;.....0.@.....\n0000020: 4000 0000 0000 0000 880d 0000 0000 0000  @...............\n0000030: 0000 0000 4000 3800 0800 4000 2500 2200  ....@.8...@.%.\".<\/code><\/pre>\n<p>\u53ef\u4ee5\u6e05\u695a\u7684\u770b\u5230\u6700\u5f00\u59cb\u56db\u4f4d\u4e2d\u7684 <code>ELF<\/code>\uff0c\u8fd9\u4e5f\u662f ELF \u683c\u5f0f\u6545\u610f\u5b9a\u4e49\u7684\u6807\u8bc6\u3002<\/p>\n<p>\u7528 <span style=\"color: #ff0000;\"><strong><code>strings\u547d\u4ee4<\/code><\/strong><\/span> \u53ef\u4ee5\u770b\u5230\u6587\u4ef6\u4e2d\u7684 C \u98ce\u683c\u5b57\u7b26\u4e32\uff08\u53ef\u7528\u4e8e\u5feb\u901f\u67e5\u770b\u67e5\u770b\u7a0b\u5e8f\u4e2d\u7684\u53ef\u6253\u5370\u5b57\u7b26\uff09\uff1a<\/p>\n<pre><code>$ strings bar | tail -4\nUH-H    `\nUH-H    `\n[]AA]A^A_\nhello world.<\/code><\/pre>\n<p>\u8fd9\u91cc\u5f88\u6e05\u695a\u7684\u5c31\u80fd\u770b\u5230\u7a0b\u5e8f\u4e2d\u7684 &#8220;hello world.&#8221;\uff0c\u53ef\u4ee5\u60f3\u8c61\u7a0b\u5e8f\u4e2d\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\u662f\u591a\u4e48\u7684\u8106\u5f31\u3002\u901a\u8fc7\u4e0a\u9762\u7684 <code>xxd<\/code> \u6211\u4eec\u8fd8\u53ef\u4ee5\u8f7b\u6613\u7684\u4fee\u6539\u5b83\uff1a<\/p>\n<pre><code>$ echo \"0005da: 6865 6c6c 6f20\" | xxd -r - bar\n$ .\/bar\nhello hello <\/code><\/pre>\n<p>\u901a\u8fc7 <code>xxd -r<\/code> \u6211\u4eec\u628a &#8220;world.&#8221; \u5bf9\u5e94\u7684\u6570\u636e\u6362\u6210\u4e86 &#8220;hello &#8220;\uff0c\u91cd\u65b0\u6267\u884c\u7a0b\u5e8f\u8f93\u51fa\u4e5f\u53d1\u751f\u4e86\u76f8\u5e94\u7684\u6539\u53d8\u3002<\/p>\n<h5>\u7f16\u8bd1\uff0c\u94fe\u63a5\u548c\u8c03\u8bd5\u76f8\u5173<\/h5>\n<p>\u7528 <span style=\"color: #ff0000;\"><strong><code>nm\u547d\u4ee4<\/code><\/strong><\/span> \u53ef\u4ee5\u5217\u51fa ELF \u4e2d\u5305\u542b\u7684 &#8220;Symbols&#8221;\uff0c\u6bd4\u8f83\u660e\u663e\u7684\u5c31\u662f\u5176\u81ea\u5df1\u5185\u90e8\u7684 C \u51fd\u6570\u540d\uff0c\u4ee5\u53ca\u7528\u5230\u5916\u90e8\u7684\u5176\u4ed6\u51fd\u6570\u3002\u7531\u4e8e\u6211\u4eec\u7f16\u8bd1\u65f6\u7279\u5730\u4fdd\u7559\u4e86\u8c03\u8bd5\u4fe1\u606f\uff0c\u7528 <code>nm -l<\/code> \u53ef\u4ee5\u770b\u5230 Symbol \u5bf9\u5e94\u7684\u5230\u54ea\u4e2a\u6587\u4ef6\u7684\u54ea\u4e00\u884c\uff1a<\/p>\n<pre><code>$ nm -l bar | tail -5\n000000000040051c T foo  \/tmp\/bar.c:3\n00000000004004f0 t frame_dummy\n000000000040052c T main \/tmp\/bar.c:7\n                 U puts@@GLIBC_2.2.5\n0000000000400490 t register_tm_clones<\/code><\/pre>\n<p>\u4e0a\u9762\u663e\u793a\u7684\u4e09\u680f\u5206\u522b\u662f Symbol \u7684\u5730\u5740\uff0c\u7c7b\u578b\u548c\u540d\u5b57\u52a0\u4e0a\u9644\u52a0\u4fe1\u606f\u3002\u7c7b\u578b\u4e2d <code>T<\/code> \u4ee3\u8868\u7b26\u53f7\u5728 ELF \u7684 <code>.text<\/code> section \u4e2d\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fd9\u4e2a\u7b26\u53f7\u7684\u4ee3\u7801\u5305\u542b\u5728\u8be5\u6587\u4ef6\u4e2d\u3002\u800c &#8220;U&#8221; \u662f\u8bf4\u8be5\u7b26\u53f7\u8fd8\u672a\u5b9a\u4e49\uff0c\u4e5f\u5c31\u662f\u9700\u8981\u8fd0\u884c\u65f6\u7531 <code>ld.so<\/code> \u94fe\u63a5\u5e76\u6ee1\u8db3\u3002<\/p>\n<p><code>bar<\/code> \u751a\u81f3\u4e8e\u7edd\u5927\u591a\u6570\u5e38\u89c1 Linux \u53ef\u6267\u884c\u6587\u4ef6\u90fd\u662f\u52a8\u6001\u94fe\u63a5\u7684\u3002<span style=\"color: #ff0000;\"><strong><code>ldd\u547d\u4ee4<\/code><\/strong><\/span> \u53ef\u4ee5\u7528\u6765\u67e5\u770b\u5176\u4f9d\u8d56\u7684\u52a8\u6001\u94fe\u63a5\u5e93\uff0c\u4e5f\u5c31\u662f\u8981\u8fd0\u884c <code>bar<\/code> \u6240\u9700\u8981\u7684\u5176\u4ed6 <code>.so<\/code> \u6587\u4ef6\u4eec\uff1a<\/p>\n<pre><code>$ ldd bar\n    linux-vdso.so.1 (0x00007fffecf1b000)\n    libc.so.6 =&gt; \/lib\/x86_64-linux-gnu\/libc.so.6 (0x00007f0e6e123000)\n    \/lib64\/ld-linux-x86-64.so.2 (0x00007f0e6e4e1000)<\/code><\/pre>\n<p>\u5176\u4e2d <code>libc.so.6<\/code> \u5c31\u662f\u4e0a\u9762\u63d0\u5230\u7684 <code>soname<\/code>\u3002<code>libc<\/code> \u4e2d\u7684\u5185\u5bb9\u662f C \u6807\u51c6\u5e93\u7684\u4e1c\u897f\u3002\u5982\u540c<a title=\"Beautiful Native Libraries\" href=\"http:\/\/lucumr.pocoo.org\/2013\/8\/18\/beautiful-native-libraries\/#which-language\" target=\"_blank\" rel=\"nofollow\">\u8fd9\u7bc7\u6587\u7ae0<\/a>\u91cc\u63d0\u5230\u7684\uff0c&#8221;C \u8bed\u8a00\u6700\u5927\u7684\u6210\u5c31\u4e4b\u4e00\u5c31\u662f\u8ba9\u5927\u5bb6\u8ba4\u4e3a C \u662f\u4e0d\u9700\u8981 Runtime \u7684&#8221;\uff0c\u4e8b\u5b9e\u4e0a\u7edd\u5927\u90e8\u5206\u7a0b\u5e8f\u90fd\u8dd1\u4e0d\u5f00\u5bf9 libc \u7684\u4f9d\u8d56\u3002\u6700\u4e0b\u9762\u7684 <code>ld-linux-x86-64.so.2<\/code>\u5c31\u662f\u4e4b\u524d\u63d0\u5230\u7684 <code>ld.so<\/code>\uff0c\u663e\u7136\u7684\u4e0d\u540c\u67b6\u6784\u4f7f\u7528\u7684 <code>ld.so<\/code> \u4e5f\u662f\u5206\u5f00\u7684\u3002\u6700\u5f00\u59cb\u7684<code>linux-vdso.so.1<\/code> \u8bf4\u5b9e\u8bdd\u6211\u4e5f\u662f\u7b2c\u4e00\u6b21\u6ce8\u610f\u5230&#8230; \u4f46<a title=\"vDSO - overview of the virtual ELF dynamic shared object\" href=\"http:\/\/man7.org\/linux\/man-pages\/man7\/vdso.7.html\" target=\"_blank\" rel=\"nofollow\">\u6839\u636e\u6587\u6863\u6765\u770b<\/a>\u4f3c\u4e4e\u662f\u4e0d\u592a\u9700\u8981\u666e\u901a\u7528\u6237\u5173\u5fc3\u7684\u4e1c\u897f\u3002<\/p>\n<p>\u6bd4\u5982\u6211\u628a <code>bar<\/code> \u62f7\u8d1d\u5230\u53e6\u4e00\u53f0\u7cfb\u7edf\u76ee\u5f55\u7ed3\u6784\u6709\u4e9b\u5dee\u5f02\u7684\u673a\u5668\u4e0a\uff0c\u90a3\u4e48\u67d0\u4e9b\u5e93\u53ef\u80fd\u627e\u4e0d\u5230\u3002<code>ldd<\/code> \u5728\u8fd9\u79cd\u65f6\u5019\u4e5f\u80fd\u7ed9\u51fa\u6bd4\u8f83\u6e05\u695a\u7684\u89e3\u91ca\u3002\u53e6\u5916 Windows \u4e0a\u4e5f\u6709\u4e00\u4e2a\u7c7b\u4f3c\u7684\u5de5\u5177\u53eb\u505a <a title=\"Dependency Walker 2.2\" href=\"http:\/\/www.dependencywalker.com\/\" target=\"_blank\" rel=\"nofollow\">depends.exe<\/a> \u7528\u6765\u67e5\u627e dll \u4f9d\u8d56\u3002<\/p>\n<p>\u8981\u786e\u5b9a <code>bar<\/code> \u5230\u5e95\u662f\u7528\u54ea\u4e2a\u7248\u672c\u7684\u7f16\u8bd1\u5668\u7f16\u8bd1\u51fa\u6765\u7684\uff0c\u7b80\u5355\u7c97\u66b4\u7684\u65b9\u6cd5\u5c31\u662f\u4f7f\u7528<code>strings -a<\/code> \u6765\u8bfb\u53d6\u5305\u62ec\u975e\u7528\u6237\u533a\u7684\u6240\u6709\u5b57\u4e32\u7b26\uff1a<\/p>\n<pre><code>$ strings -a bar | grep GCC\nGCC: (Debian 4.7.2-5) 4.7.2\nGCC: (Debian 4.8.2-21) 4.8.2<\/code><\/pre>\n<p>\u4e8b\u5b9e\u4e0a\u8fd9\u91cc\u7528\u7684\u7684\u786e\u662f 4.7.2\uff0c\u6211\u4e5f\u8bf4\u4e0d\u6e05\u540e\u9762\u7684 4.8 \u662f\u4ece\u54ea\u91cc\u6765\u7684\u3002\u8981\u6ce8\u610f\u7684\u662f\u8fd9\u4e9b\u4fe1\u606f\u57fa\u672c\u4e0a\u662f\u53ea\u5728 ELF \u6587\u4ef6\u5e26\u6709\u8c03\u8bd5\u4fe1\u606f\u7684\u65f6\u5019\u624d\u4f1a\u4fdd\u7559\u4e0b\u6765\u3002<\/p>\n<p>\u8c03\u8bd5\u4fe1\u606f\u5f53\u7136\u4e5f\u662f\u76f4\u63a5\u5b58\u5728 ELF \u6587\u4ef6\u4e2d\u7684\u3002ELF \u7528\u4e0d\u540c\u7684 <strong>section<\/strong> \u6765\u5b58\u653e\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5728\u6267\u884c\u7684\u65f6\u5019\u5c06 <strong>section<\/strong> \u62fc\u6210 <strong>segment<\/strong> \u6765\u8f7d\u5165\u5230\u5185\u5b58\u3002\u8fd9\u91cc\u7684<strong>section<\/strong> \u503c\u7684\u5c31\u662f <code>.text<\/code>, <code>.data<\/code> \u8fd9\u6837\u7684\u6570\u636e\u6bb5\u3002\u800c <strong>segment<\/strong> \u6b63\u662f\u6211\u4eec\u719f\u6089\u7684<code>Segment Fault<\/code> \u91cc\u9762\u7684\u90a3\u4e2a <strong>segment<\/strong>\u3002\u6211\u4eec\u53ef\u4ee5\u7528\u5de5\u5177\u8bfb\u53d6 ELF \u6587\u4ef6\u5e76\u89e3\u6790\u5176\u5185\u5bb9\u3002\u8fd9\u91cc\u53ef\u4ee5\u4f7f\u7528 <code>readelf<\/code> \u548c <code>objdump<\/code>\uff0c\u4e24\u8005\u529f\u80fd\u4e0a\u4f3c\u4e4e\u6ca1\u6709\u592a\u5927\u7684\u533a\u522b\uff0c\u672c\u6587\u540e\u9762\u7528 <code>readelf<\/code> \u4f5c\u4e3a\u793a\u8303\u3002<\/p>\n<p>\u7528 <code>readelf -l<\/code> \u5217\u51fa\u6240\u6709\u7684 <strong>segment<\/strong> \u548c\u5176\u5305\u542b\u7684 <strong>section<\/strong> \u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u7531\u4e8e\u663e\u793a\u7ed3\u679c\u6bd4\u8f83\u957f\uff0c\u8fd9\u91cc\u53ea\u5217\u51fa\u88c1\u51cf\u8fc7\u7684\u4e00\u90e8\u5206\uff1a<\/p>\n<pre><code>$ readelf -l bar\nElf file type is EXEC (Executable file)\nEntry point 0x400430\nThere are 8 program headers, starting at offset 64\n\nProgram Headers:\n  Type           Offset             VirtAddr           PhysAddr\n                 FileSiz            MemSiz              Flags  Align\n                 .............................\n  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000\n                 0x0000000000000704 0x0000000000000704  R E    200000\n  LOAD           0x0000000000000708 0x0000000000600708 0x0000000000600708\n                 0x0000000000000240 0x0000000000000248  RW     200000\n                 .............................\n\n Section to Segment mapping:\n  Segment Sections...\n                 .............................\n   02     .plt .text .fini .rodata\n   03     .data .bss\n                 .............................<\/code><\/pre>\n<p>\u9996\u5148\u6765\u770b\u770b <strong>section<\/strong>\uff0c\u8fd9\u4e2a\u4f30\u8ba1\u5927\u5bb6\u90fd\u6709\u4e00\u70b9\u5370\u8c61\uff1a<\/p>\n<ul>\n<li><code>.text<\/code> \u5b58\u653e\u7684\u662f\u7f16\u8bd1\u8fc7\u540e\u7684\u4ee3\u7801\uff0c\u4e5f\u5c31\u662f\u53ef\u4ee5\u6267\u884c\u7684\u673a\u5668\u6307\u4ee4\u3002<\/li>\n<li><code>.rodata<\/code> \u5b58\u653e\u7684\u662f\u53ea\u8bfb\u7684\u6570\u636e\uff0c\u6bd4\u5982\u50cf\u6211\u4eec\u7a0b\u5e8f\u4e2d\u7684 &#8220;hello world&#8221; \u5e38\u91cf\u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>.data<\/code> \u5b58\u653e\u7684\u662f\u53ef\u8bfb\u5199\u7684\u6570\u636e\uff0c\u6bd4\u5982 <code>int foo<\/code> \u8fd9\u6837\u7684\u5168\u5c40\u53d8\u91cf\u3002<\/li>\n<li><code>.bss<\/code> \u5b58\u653e\u7684\u662f\u672a\u521d\u59cb\u5316\u7684\u6570\u636e\u6bb5\u3002<\/li>\n<\/ul>\n<p>\u5bf9\u5e94\u7684\u6211\u4eec\u6709\u4e24\u4e2a <strong>section<\/strong> \u5206\u522b\u662f <code>02<\/code> \u548c <code>03<\/code>\uff0c\u4e24\u8005\u7684\u6700\u5927\u5dee\u522b\u5728\u4e8e <code>FLAGS<\/code> \u90a3\u4e00\u680f\u3002\u8fd9\u91cc\u7684 <code>FLAGS<\/code> \u8ddf Linux \u6587\u4ef6\u7cfb\u7edf\u7684\u6743\u9650\u662f\u4e00\u4e2a\u610f\u601d\uff0c<code>RWE<\/code> \u5206\u522b\u5bf9\u5e94\u53ef\u8bfb\uff0c\u53ef\u5199\u548c\u53ef\u884c\u3002<code>02<\/code> \u662f\u53ef\u8bfb\u53ef\u6267\u884c\u4e0d\u80fd\u5199\u7684\uff0c\u6240\u4ee5 <code>.text<\/code>, <code>.rodata<\/code> \u4e24\u4e2a <strong>section<\/strong> \u88ab\u5206\u914d\u5230\u4e86\u8fd9\u4e2a <strong>segment<\/strong>\u3002\u7cfb\u7edf\u5728\u8f7d\u5165 <code>02<\/code> \u65f6\u4f1a\u6839\u636e FLAGS \u5bf9\u5176\u8fdb\u884c\u4fdd\u62a4\uff0c\u5982\u679c\u4f60\u6570\u7ec4\u8d8a\u754c\u5199\u5230\u4e86 <code>02<\/code> \u8fd9\u4e2a\u4e0d\u53ef\u5199\u7684 <strong>segment<\/strong> \u4e0a\uff0c\u5c31\u4f1a\u4ea7\u751f <code>Segment Fault<\/code>\u3002\u540c\u7406\u56e0\u4e3a<code>.data<\/code> \u548c <code>.bss<\/code> \u90fd\u662f\u9700\u8981\u53ef\u4ee5\u88ab\u5199\u7684\uff0c\u6240\u4ee5\u4ed6\u4eec\u88ab\u5206\u914d\u5230\u4e86 <code>03<\/code> \u4e0a\u3002\u663e\u7136\u5982\u679c <code>03<\/code> \u4e2d\u7684\u6570\u636e\u80fd\u88ab\u5f53\u505a\u6307\u4ee4\u6267\u884c\u4f1a\u5f02\u5e38\u5371\u9669\uff0c\u6240\u4ee5\u7cfb\u7edf\u4e5f\u4f1a\u4fdd\u8bc1\u5982\u679c <a title=\"Program Counter on Wikipedia\" href=\"http:\/\/en.wikipedia.org\/wiki\/Program_counter\" target=\"_blank\" rel=\"nofollow\">PC<\/a> \u6307\u5230\u8fd9\u8fb9\u7684\u8bdd\u4e5f\u4f1a\u62a5\u9519\u3002<\/p>\n<p><code>readelf<\/code> \u4e5f\u53ef\u4ee5\u7528\u6765\u67e5\u770b\u67d0\u4e2a <strong>section<\/strong> \u4e2d\u7684\u5185\u5bb9\u3002\u5f88\u5bb9\u6613\u60f3\u8c61 <code>.rodata<\/code> \u4e2d\u5e94\u8be5\u6709 &#8220;hello world.&#8221; \u8fd9\u4e2a\u5b57\u7b26\u4e32\uff0c\u8fd9\u91cc\u7528 <code>readelf -x .rodata<\/code> \u6765\u786e\u8ba4\u4e00\u4e0b\uff1a<\/p>\n<pre><code>$ readelf -x .rodata bar\n\nHex dump of section '.rodata':\n  0x004005d0 01000200 68656c6c 6f20776f 726c642e ....hello world.\n  0x004005e0 00                                  .<\/code><\/pre>\n<p>\u4e8b\u5b9e\u4e0a\u4e0d\u662f\u6240\u6709\u7684 <strong>section<\/strong> \u90fd\u4f1a\u88ab\u5206\u5230 <strong>segment<\/strong> \u5e76\u8f7d\u5165\u5185\u5b58\u3002\u7528 <code>readelf -S<\/code> \u53ef\u4ee5\u5217\u51fa\u6240\u6709\u7684 <strong>section<\/strong>\uff1a<\/p>\n<pre><code>$ readelf -S bar\nThere are 37 section headers, starting at offset 0xd88:\n\nSection Headers:\n  [Nr] Name              Type             Address           Offset\n       Size              EntSize          Flags  Link  Info  Align\n  [ 0]                   NULL             0000000000000000  00000000\n       0000000000000000  0000000000000000           0     0     0\n                 .............................\n  [12] .init             PROGBITS         00000000004003d0  000003d0\n       000000000000001a  0000000000000000  AX       0     0     4\n  [13] .plt              PROGBITS         00000000004003f0  000003f0\n       0000000000000040  0000000000000010  AX       0     0     16\n  [14] .text             PROGBITS         0000000000400430  00000430\n       0000000000000194  0000000000000000  AX       0     0     16\n  [15] .fini             PROGBITS         00000000004005c4  000005c4\n       0000000000000009  0000000000000000  AX       0     0     4\n  [16] .rodata           PROGBITS         00000000004005d0  000005d0\n       0000000000000011  0000000000000000   A       0     0     4\n                 .............................\n  [25] .data             PROGBITS         0000000000600938  00000938\n       0000000000000010  0000000000000000  WA       0     0     8\n  [26] .bss              NOBITS           0000000000600948  00000948\n       0000000000000008  0000000000000000  WA       0     0     4\n  [27] .comment          PROGBITS         0000000000000000  00000948\n       0000000000000039  0000000000000001  MS       0     0     1\n  [28] .debug_aranges    PROGBITS         0000000000000000  00000981\n       0000000000000030  0000000000000000           0     0     1\n  [29] .debug_info       PROGBITS         0000000000000000  000009b1\n       00000000000000b2  0000000000000000           0     0     1<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u50cf <code>.comment<\/code> \u548c debug \u76f8\u5173\u7684 <strong>section<\/strong> \u660e\u663e\u6ca1\u6709\u88ab\u5206\u5230\u4efb\u4f55 <strong>segment<\/strong>\u91cc\u3002<code>.comment<\/code> \u91cc\u9762\u653e\u7684\u6b63\u662f\u4e4b\u524d\u770b\u5230\u7684\u7f16\u8bd1\u5668\u4fe1\u606f\uff1a<\/p>\n<pre><code>$ readelf -x .comment bar\n\nHex dump of section '.comment':\n  0x00000000 4743433a 20284465 6269616e 20342e37 GCC: (Debian 4.7\n  0x00000010 2e322d35 2920342e 372e3200 4743433a .2-5) 4.7.2.GCC:\n  0x00000020 20284465 6269616e 20342e38 2e322d32  (Debian 4.8.2-2\n  0x00000030 31292034 2e382e32 00                1) 4.8.2.<\/code><\/pre>\n<p>\u800c\u5404\u79cd debug \u76f8\u5173\u7684\u4fe1\u606f\u5219\u9700\u8981 <code>gdb<\/code> \u8fd9\u6837\u7684\u5de5\u5177\u6765\u8bfb\u53d6\u5e76\u89e3\u6790\uff1a<\/p>\n<pre><code>$ gdb -batch -ex \"info sources\" bar\nSource files for which symbols will be read in on demand:\n\/tmp\/bar.c<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230 <code>gdb<\/code> \u80fd\u591f\u627e\u5230\u6e90\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/p>\n<p>\u4f7f\u7528 <code>strip<\/code> \u53ef\u4ee5\u5c06\u8c03\u8bd5\u4fe1\u606f\u4ece ELF \u4e2d\u6d88\u53bb:<\/p>\n<pre><code>$ strip bar\n$ readelf -S bar\nThere are 29 section headers, starting at offset 0xa80:\n                 .............................<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230 <strong>section<\/strong> \u6570\u91cf\u51cf\u5c11\u4e86\u5f88\u591a\u3002<\/p>\n<h5>\u6700\u540e<\/h5>\n<p>\u4e0d\u77e5\u9053\u4f60\u6709\u6ca1\u6709\u60f3\u5230\u8fd9\u4e9b\u4e1c\u897f\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\u6709\u4ec0\u4e48\u7528\u5904\u3002\u8bf4\u5b9e\u8bdd\u8fd9\u4e9b\u53ef\u4ee5\u7528\u7684\u5730\u65b9\u4e0d\u662f\u592a\u591a&#8230; \u4f46\u5076\u5c14\u78b0\u9700\u8981\u5904\u7406\u73af\u5883\u76f8\u5173\u7684\u60c5\u51b5\uff0c\u6216\u8005\u9a8c\u8bc1\u7f16\u8bd1\u51fa\u7684\u6587\u4ef6\u662f\u5426\u6709\u95ee\u9898\uff0c\u8fd9\u4e9b\u4e1c\u897f\u5e94\u8be5\u80fd\u6d3e\u7684\u4e0a\u7528\u573a\u3002<\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u539f\u6587\u5730\u5740<\/strong><\/span>\uff1a<a href=\"http:\/\/www.jianshu.com\/p\/0e0005cc0928\" target=\"_blank\">http:\/\/www.jianshu.com\/p\/0e0005cc0928<\/a><\/p>\n<p>\u4e4b\u524d\u4e5f\u7528\u5230\u8fc7\u5176\u4e2d\u7684\u5927\u90e8\u5206\u547d\u4ee4\uff0c\u4f46\u6ca1\u6709\u8fdb\u884c\u8fc7\u7cfb\u7edf\u6027\u7684\u603b\u7ed3\u548c\u6574\u7406\uff0c<a href=\"http:\/\/www.jianshu.com\/users\/emNpEA\" target=\"_blank\">\u539f\u4f5c\u8005<\/a>\u7684\u5206\u4eab\u8ba9\u6211\u53d7\u76ca\u532a\u6d45\u3002<\/p>\n<h6>\u66f4\u591a\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/blog.chinaunix.net\/uid-9525959-id-2001833.html\" target=\"_blank\">Linux\u547d\u4ee4\u5b66\u4e60\u624b\u518c-readelf\u547d\u4ee4<\/a><\/li>\n<li><a href=\"http:\/\/linux.about.com\/library\/cmd\/blcmdl1_readelf.htm\" target=\"_blank\">readelf &#8211; Linux Command<\/a><\/li>\n<li><a href=\"http:\/\/man.linuxde.net\/readelf\" target=\"_blank\">readelf\u547d\u4ee4_Linux readelf \u547d\u4ee4\u7528\u6cd5\u8be6\u89e3<\/a><\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/wocjj\/article\/details\/8100279\" target=\"_blank\">linux \u4f7f\u7528readelf\u547d\u4ee4\u67e5\u770b.so\u52a8\u6001\u5e93\u7684\u771f\u5b9e\u540d<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u56e0\u4e3a\u4e0d\u77e5\u9053\u600e\u4e48\u53d6\u597d\uff0c\u6240\u4ee5\u5c31\u7528\u4e86\u4e2a\u53e3\u8bed\u5316\u7684\u5185\u5bb9\u5927\u81f4\u8868\u8fbe\u51fa\u610f\u601d\u5c31\u884c\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u4e00\u4e9b\u547d\u4ee4\u5728\u4e4b\u524d\u7684\u6587\u7ae0\u4e2d\u4e5f\u6709\u6d89\u53ca [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,12],"tags":[30,323,71],"class_list":["post-1157","post","type-post","status-publish","format-standard","hentry","category-linux","category-tools","tag-linux","tag-readelf","tag-tools"],"views":7979,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=1157"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1157\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}