{"id":321,"date":"2024-03-28T00:24:03","date_gmt":"2024-03-27T16:24:03","guid":{"rendered":"https:\/\/cococat.top\/?p=321"},"modified":"2024-03-28T00:24:03","modified_gmt":"2024-03-27T16:24:03","slug":"ld-so","status":"publish","type":"post","link":"https:\/\/cococat.top\/index.php\/2024\/03\/28\/ld-so\/","title":{"rendered":"ld.so \u76f8\u5173\u77e5\u8bc6"},"content":{"rendered":"<h2>1. \u5f15\u5b50<\/h2>\n<p>\u4eca\u5929\u5728\u5de5\u4f5c\u4e2d\u7528 <code>ldd<\/code> \u68c0\u67e5 <code>tengine<\/code> \u7684\u53ef\u6267\u884c\u6587\u4ef6\u65f6\uff0c\u51fa\u4e8e\u597d\u5947\u770b\u4e86\u4e00\u4e0b\u91cc\u9762\u6d89\u53ca\u7684 <code>ld-linux-x86-64.so.2 <\/code> \u52a8\u6001\u94fe\u63a5\u5e93\uff0c\u7136\u540e\u67e5\u5230\u4e86\u8fd9\u6837\u4e00\u4e2a\u795e\u5947\u7684\u95ee\u7b54\uff1a<\/p>\n<p><a href=\"https:\/\/unix.stackexchange.com\/questions\/400621\/what-is-lib64-ld-linux-x86-64-so-2-and-why-can-it-be-used-to-execute-file\">https:\/\/unix.stackexchange.com\/questions\/400621\/what-is-lib64-ld-linux-x86-64-so-2-and-why-can-it-be-used-to-execute-file<\/a><\/p>\n<p>\u6211\u4eec\u5e73\u5e38\u4e60\u60ef\u4e8e\u7528 <code>chmod<\/code> \u53bb\u66f4\u6539\u6587\u4ef6\u7684\u6743\u9650\uff0c\u4f46\u662f\u4ece\u6765\u6ca1\u6709\u60f3\u8fc7\u4e00\u4e2a\u95ee\u9898\uff1a\u5982\u679c\u7531\u4e8e\u4e00\u4e9b\u8bef\u64cd\u4f5c\u800c\u8ba9 <code>chmod<\/code> \u672c\u8eab\u7684 X \u6743\u9650\u6ca1\u6709\u4e86\uff0c\u90a3\u8be5\u600e\u4e48\u6551\u56de\u6765\uff1f\u8fd9\u4e2a\u95ee\u7b54\u7ed9\u51fa\u4e86\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<pre><code class=\"language-bash\">\/lib64\/ld-linux-x86-64.so.2 \/bin\/chmod +x \/bin\/chmod<\/code><\/pre>\n<p>\u4e4d\u4e00\u770b\u7b80\u76f4\u662f\u9b54\u6cd5\uff0c\u5728\u523b\u677f\u5370\u8c61\u91cc\u52a8\u6001\u94fe\u63a5\u5e93\u6587\u4ef6 <code>.so<\/code> \u6839\u672c\u4e0d\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\uff0c\u8fd9\u91cc\u4e0d\u4ec5\u76f4\u63a5\u8fd0\u884c\u4e86\u4e00\u4e2a\u52a8\u6001\u94fe\u63a5\u5e93\u6587\u4ef6\uff0c\u8fd8\u7ed9\u5b83\u4f20\u5165\u4e86\u53c2\u6570\u3002<\/p>\n<h2>2. \u539f\u7406<\/h2>\n<h3>2.1. ld.so \u7b80\u4ecb<\/h3>\n<p>\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u65e0\u53c2\u8fd0\u884c <code>ld-linux-x86-64.so.2<\/code> \uff08\u5728 <code>centos<\/code> \u884d\u751f\u7684\u53d1\u884c\u7248\u4e0a\u53ef\u4ee5\u65e0\u53c2\uff0c\u5728 Ubuntu \u4e0a\u9700\u8981\u52a0 <code>--help<\/code> \uff09\uff0c\u5c31\u4f1a\u5f97\u5230\u5982\u4e0b\u7684\u8f93\u51fa\uff1a<\/p>\n<pre><code>You have invoked &#039;ld.so&#039;, the program interpreter for dynamically-linked ELF programs.  Usually, the program interpreter is invoked automatically when a dynamically-linked executable is started.\n\nYou may invoke the program interpreter program directly from the command line to load and run an ELF executable file; this is like executing that file itself, but always uses the program interpreter you invoked, instead of the program interpreter specified in the executable file you run.  Invoking the program interpreter directly provides access to additional diagnostics, and changing the dynamic linker behavior without setting environment variables (which would be inherited by subprocesses).\n\n[\u7701\u7565\u4e00\u4e9b\u53c2\u6570\u8bf4\u660e]<\/code><\/pre>\n<p>\u4ece\u8fd9\u4e9b\u4fe1\u606f\u4e2d\u6211\u4eec\u53ef\u4ee5\u5f97\u77e5\uff0c\u8fd9\u4e2a <code>ld.so<\/code> \u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u52a8\u6001\u94fe\u63a5\u7a0b\u5e8f\u7684\u201c\u89e3\u91ca\u5668\u201d\uff08interpreter\uff09\uff0c\u800c\u5f53\u6211\u4eec\u6267\u884c\u4e00\u4e2a\u52a8\u6001\u94fe\u63a5\u7a0b\u5e8f\u65f6\uff0c\u7b49\u6548\u7684\u547d\u4ee4\u5b9e\u9645\u4e0a\u662f\u628a\u539f\u59cb\u547d\u4ee4\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9\u8fd9\u4e2a .so\u3002\u5373\u5f53\u6211\u4eec\u5728\u7ec8\u7aef\u91cc\u6267\u884c <code>chmod<\/code> \u65f6\uff0c\u5b9e\u9645\u4e0a\u6267\u884c\u7684\u547d\u4ee4\u662f <code>ld-linux-x86-64.so.2 \/bin\/chmod<\/code> \uff0c\u800c\u5176\u4f5c\u7528\u662f\u52a0\u8f7d\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\u6240\u9700\u7684\u6240\u6709 .so\uff0c\u4ece\u800c\u4e3a\u52a8\u6001\u94fe\u63a5\u7a0b\u5e8f\u7684\u8fd0\u884c\u63d0\u4f9b\u6761\u4ef6\u3002<\/p>\n<blockquote>\n<p>\u6b64\u5916\uff0c\u5b9e\u9645\u4e0a\u8fd8\u6709\u5f88\u591a .so \u90fd\u662f\u53ef\u4ee5\u8fd0\u884c\u7684\uff0c\u4f8b\u5982\u5e38\u89c1\u7684 <code>libc.so<\/code> \uff0c\u76f4\u63a5\u8fd0\u884c\u4e5f\u53ef\u4ee5\u8f93\u51fa\u4e00\u6bb5\u7248\u672c\u548c\u8bf4\u660e\u4fe1\u606f\u3002\u6839\u636e<a href=\"https:\/\/unix.stackexchange.com\/questions\/223385\/why-and-how-are-some-shared-libraries-runnable-as-though-they-are-executables\">\u7f51\u4e0a\u7684\u8bf4\u660e<\/a>\uff0c\u53ea\u8981\u5e93\u4e2d\u5305\u542b main() \u51fd\u6570\u5373\u53ef\u3002<\/p>\n<\/blockquote>\n<h3>2.2. ld.so \u7684\u8be6\u7ec6\u4ecb\u7ecd<\/h3>\n<p>TODO: <a href=\"https:\/\/www.man7.org\/linux\/man-pages\/man8\/ld.so.8.html\">https:\/\/www.man7.org\/linux\/man-pages\/man8\/ld.so.8.html<\/a><\/p>\n<h3>2.3. ld \u4e0e ld.so<\/h3>\n<p>\u7f16\u8bd1\u65f6\uff0c\u94fe\u63a5\u5668 <code>ld<\/code> \u662f\u6211\u4eec\u719f\u6089\u7684\u8001\u670b\u53cb\uff0c\u5bf9\u4e8e\u6240\u6709\u53c2\u4e0e\u94fe\u63a5\u7684\u53ef\u91cd\u5b9a\u5411\u76ee\u6807\u6587\u4ef6\uff08.o\uff09\uff0c<code>ld<\/code>\u4f1a\u6267\u884c\u7b26\u53f7\u89e3\u6790\uff08\u5c06\u5bf9\u7b26\u53f7\u7684\u5f15\u7528\u4e0e\u7b26\u53f7\u8868\u4e2d\u7684\u552f\u4e00\u786e\u5b9a\u7b26\u53f7\u5173\u8054\u8d77\u6765\uff09\u4e0e\u91cd\u5b9a\u5411\uff08\u5c06\u5404\u4e2a\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u6bb5\u3001\u6570\u636e\u6bb5\u5206\u522b\u5408\u5e76\uff0c\u968f\u540e\u5373\u53ef\u8ba1\u7b97\u6bcf\u4e2a\u7b26\u53f7\u5728\u865a\u62df\u5185\u5b58\u4e2d\u7684\u5730\u5740\uff0c\u518d\u628a\u6240\u6709\u7684\u7b26\u53f7\u5f15\u7528\u4fee\u6539\u4e3a\u8fd9\u4e2a\u5730\u5740\uff09\u3002\u8fd9\u90e8\u5206\u53ef\u4ee5\u91cd\u65b0\u8bfb\u4e0b <a href=\"https:\/\/hansimov.gitbook.io\/csapp\/part2\/ch07-linking\/7.7-relocation\">CSAPP<\/a><\/p>\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u5bf9\u4e8e\u52a8\u6001\u94fe\u63a5\u7a0b\u5e8f\uff0c<code>ld<\/code> \u4f5c\u7528\u4e8e\u7f16\u8bd1\u671f\uff0c\u800c <code>ld.so<\/code> \u4f5c\u7528\u4e8e\u8fd0\u884c\u65f6\u3002<\/p>\n<h2>3. <code>execve()<\/code>\u7cfb\u7edf\u8c03\u7528<\/h2>\n<p>\u5728 stackexchange \u7684\u539f\u56de\u7b54\u91cc\uff0c\u7b54\u4e3b\u63d0\u5230\u4e86 <code>execve()<\/code> \u8fd9\u4e2a syscall\uff0c\u8fd9\u91cc\u4e5f\u7a0d\u5fae\u5b66\u4e60\u4e0b\u3002\u5176\u7cfb\u7edf\u8c03\u7528\u7684\u51fd\u6570\u7b7e\u540d\u662f\uff1a<\/p>\n<pre><code class=\"language-c\">int execve(const char *pathname, char *const argv[], char *const envp[]);<\/code><\/pre>\n<p>\u8fd9\u4e2a\u7cfb\u7edf\u8c03\u7528\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u628a <code>pathname<\/code> \u6307\u5b9a\u7684\u7a0b\u5e8f\u52a0\u8f7d\u5230\u5f53\u524d\u8fdb\u7a0b\u7684\u5185\u5b58\u7a7a\u95f4\u5185\uff0c\u5c06\u5f53\u524d\u8fdb\u7a0b\u7684\u5806\u3001\u6808\u548c\u6240\u6709\u7684\u6bb5\u6570\u636e\u90fd\u7528\u65b0\u8fdb\u7a0b\u7684\u76f8\u5e94\u90e8\u5206\u4ee3\u66ff\uff0c\u7136\u540e\u4ece\u65b0\u7a0b\u5e8f\u7684\u521d\u59cb\u5316\u4ee3\u7801\u548c <code>main<\/code> \u51fd\u6570\u5f00\u59cb\u8fd0\u884c\u3002\u540c\u65f6\uff0c\u8fdb\u7a0b\u7684ID\u5c06\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165\u547d\u4ee4\u65f6\uff0cshell \u4f1a <code>fork()<\/code> \u4e00\u4e2a\u5b50\u8fdb\u7a0b\u51fa\u6765\uff0c\u968f\u540e\u8c03\u7528 <code>execve()<\/code> \u6765\u6267\u884c\u6211\u4eec\u8f93\u5165\u7684\u547d\u4ee4\u3002\u867d\u7136\u6b64 syscall \u4f1a\u8fdb\u884c\u6267\u884c\u6743\u9650\u68c0\u67e5\uff0c\u4f46\u662f\u7531\u4e8e\u8fd9\u91cc\u68c0\u67e5\u7684\u5bf9\u8c61\u662f <code>ld.so<\/code>\uff0c\u56e0\u6b64\u68c0\u67e5\u901a\u8fc7\u3002<\/p>\n<h2>4. \u64cd\u4f5c\u7cfb\u7edf\u662f\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u662f\u9759\u6001\u94fe\u63a5\u8fd8\u662f\u52a8\u6001\u94fe\u63a5\u7684<\/h2>\n<p>\u8fd9\u5c31\u8981\u6d89\u53ca\u5230 ELF \u6587\u4ef6\u683c\u5f0f\u4e86\uff0c\u8be6\u60c5\u8bf7\u53c2\u8003<a href=\"https:\/\/cococat.top\/index.php\/2024\/04\/18\/elf\/\">\u53e6\u4e00\u7bc7\u535a\u5ba2<\/a><\/p>\n<h2>5. \u53c2\u8003<\/h2>\n<p><a href=\"https:\/\/unix.stackexchange.com\/questions\/400621\/what-is-lib64-ld-linux-x86-64-so-2-and-why-can-it-be-used-to-execute-file\">https:\/\/unix.stackexchange.com\/questions\/400621\/what-is-lib64-ld-linux-x86-64-so-2-and-why-can-it-be-used-to-execute-file<\/a><br \/>\n<a href=\"https:\/\/unix.stackexchange.com\/questions\/223385\/why-and-how-are-some-shared-libraries-runnable-as-though-they-are-executables\">https:\/\/unix.stackexchange.com\/questions\/223385\/why-and-how-are-some-shared-libraries-runnable-as-though-they-are-executables<\/a><br \/>\n<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/execve.2.html\">https:\/\/man7.org\/linux\/man-pages\/man2\/execve.2.html<\/a><br \/>\n<a href=\"https:\/\/uclibc.org\/docs\/elf-64-gen.pdf\">https:\/\/uclibc.org\/docs\/elf-64-gen.pdf<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u5f15\u5b50 \u4eca\u5929\u5728\u5de5\u4f5c\u4e2d\u7528 ldd \u68c0\u67e5 tengine \u7684\u53ef\u6267\u884c\u6587\u4ef6\u65f6\uff0c\u51fa\u4e8e\u597d\u5947\u770b\u4e86\u4e00\u4e0b\u91cc\u9762\u6d89\u53ca\u7684 ld-l [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[19,20,27],"class_list":["post-321","post","type-post","status-publish","format-standard","hentry","category-linux","tag-ld","tag-linux","tag-27"],"_links":{"self":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/321","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/comments?post=321"}],"version-history":[{"count":0,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/321\/revisions"}],"wp:attachment":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/media?parent=321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/categories?post=321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/tags?post=321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}