{"id":329,"date":"2023-12-10T11:56:30","date_gmt":"2023-12-10T03:56:30","guid":{"rendered":"https:\/\/boostup.cf\/?p=329"},"modified":"2023-12-10T15:59:17","modified_gmt":"2023-12-10T07:59:17","slug":"%e8%a6%86%e7%9b%96glibc%e4%b8%ad%e7%9a%84%e5%87%bd%e6%95%b0%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/189505.xyz\/?p=329","title":{"rendered":"\u8986\u76d6glibc\u4e2d\u7684\u51fd\u6570\u5b9e\u73b0"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_40 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" area-label=\"ez-toc-toggle-icon-1\"><label for=\"item-6a1caa1b7f870\" aria-label=\"Table of Content\"><span style=\"display: flex;align-items: center;width: 35px;height: 30px;justify-content: center;direction:ltr;\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/label><input  type=\"checkbox\" id=\"item-6a1caa1b7f870\"><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/189505.xyz\/?p=329\/#%E8%A6%86%E7%9B%96libc%E4%B8%AD%E7%9A%84malloc_%E7%B1%BB%E4%BC%BChttps_dongbotechpost2020override-glibc-func\" title=\"\n\u8986\u76d6libc\u4e2d\u7684malloc, \n\u7c7b\u4f3chttps:\/\/dongbo.tech\/post\/2020\/override-glibc-func\/ \n\">\n\u8986\u76d6libc\u4e2d\u7684malloc, \n\u7c7b\u4f3chttps:\/\/dongbo.tech\/post\/2020\/override-glibc-func\/ \n<\/a><ul class='ez-toc-list-level-2'><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/189505.xyz\/?p=329\/#%E9%9D%99%E6%80%81\" title=\"\n\u9759\u6001 \n\">\n\u9759\u6001 \n<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/189505.xyz\/?p=329\/#%E5%8F%AAlink%E8%87%AA%E5%AE%9A%E4%B9%89\" title=\"\n\u53ealink\u81ea\u5b9a\u4e49 \n\">\n\u53ealink\u81ea\u5b9a\u4e49 \n<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/189505.xyz\/?p=329\/#%E5%95%A5%E9%83%BD%E4%B8%8D%E5%81%9A\" title=\"\n\u5565\u90fd\u4e0d\u505a \n\">\n\u5565\u90fd\u4e0d\u505a \n<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/189505.xyz\/?p=329\/#LD_PRELOAD%E5%95%A5%E9%83%BD%E4%B8%8Dlink\" title=\"\nLD_PRELOAD+\u5565\u90fd\u4e0dlink \n\">\nLD_PRELOAD+\u5565\u90fd\u4e0dlink \n<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/189505.xyz\/?p=329\/#%E5%85%88link_libc%E5%86%8Dlink_malloc\" title=\"\n\u5148link libc\u518dlink malloc \n\">\n\u5148link libc\u518dlink malloc \n<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/189505.xyz\/?p=329\/#LD_PRELOAD%E5%9C%A8%E5%85%88link_libc%E5%86%8Dlink_malloc%E6%97%B6%E7%9A%84%E8%A1%A8%E7%8E%B0\" title=\"\nLD_PRELOAD\u5728\u5148link libc\u518dlink malloc\u65f6\u7684\u8868\u73b0 \n\">\nLD_PRELOAD\u5728\u5148link libc\u518dlink malloc\u65f6\u7684\u8868\u73b0 \n<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/189505.xyz\/?p=329\/#%E7%9B%B8%E5%90%8C%E5%87%BD%E6%95%B0%E7%AD%BE%E5%90%8D%E7%9A%84%E5%A4%9A%E4%B8%AA%E5%AE%9E%E7%8E%B0%E4%B8%8D%E4%B8%80%E5%AE%9A%E4%BC%9A%E6%8A%A5multiple_definition%E7%9A%84%E9%94%99\" title=\"\n\u76f8\u540c\u51fd\u6570\u7b7e\u540d\u7684\u591a\u4e2a\u5b9e\u73b0\u4e0d\u4e00\u5b9a\u4f1a\u62a5multiple definition\u7684\u9519 \n\">\n\u76f8\u540c\u51fd\u6570\u7b7e\u540d\u7684\u591a\u4e2a\u5b9e\u73b0\u4e0d\u4e00\u5b9a\u4f1a\u62a5multiple definition\u7684\u9519 \n<\/a><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E8%A6%86%E7%9B%96libc%E4%B8%AD%E7%9A%84malloc_%E7%B1%BB%E4%BC%BChttps_dongbotechpost2020override-glibc-func\"><\/span>\n\u8986\u76d6libc\u4e2d\u7684malloc,<br \/>\n\u7c7b\u4f3chttps:\/\/dongbo.tech\/post\/2020\/override-glibc-func\/<br \/>\n<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>main.c<\/p>\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nint main(int argc, char **argv) {\n        int *p = (int*)malloc(10 *sizeof(int));\n        if (p) {\n          for (int i = 0; i &lt; 10; ++i) {\n            p[i] = i;\n          }\n        }\n        return 0;\n}<\/code><\/pre>\n<p>malloc.c<\/p>\n<pre><code>#include&lt;stdio.h&gt;\n#include &lt;unistd.h&gt;\n#include&lt;stddef.h&gt;\nint arr[100000];\nvoid *malloc(size_t size) {\n  write(STDOUT_FILENO, &quot;my\\n&quot;, 3);\n  return (void*)arr;\n}\n<\/code><\/pre>\n<p>makefile<\/p>\n<pre><code>.PHONY:main\n\nmain:main.c malloc.c\n    gcc -g -c malloc.c -o malloc.o\n    gcc -g -c main.c -o main.o\n    gcc main.o malloc.o  -o main_static_o\n    gcc -g -fPIC -shared -o libmalloc.so malloc.c\n    gcc -L. -o  main_dynamic main.c -lmalloc\n    gcc -o  main_not_touch main.c\n    gcc -L. -o  main_dynamic_glibc_first main.o -lc -lmalloc\n<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"%E9%9D%99%E6%80%81\"><\/span>\n\u9759\u6001<br \/>\n<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u8c03\u7528\u81ea\u5b9a\u4e49<\/p>\n<pre><code>D.Gen|icefire|2023-12-10 11:58:45[ice@ override-libc-new].\/main_static\nmy<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"%E5%8F%AAlink%E8%87%AA%E5%AE%9A%E4%B9%89\"><\/span>\n\u53ealink\u81ea\u5b9a\u4e49<br \/>\n<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code>D.Gen|icefire|2023-12-10 11:59:01[ice@ override-libc-new].\/main_dynamic\nmy<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"%E5%95%A5%E9%83%BD%E4%B8%8D%E5%81%9A\"><\/span>\n\u5565\u90fd\u4e0d\u505a<br \/>\n<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u8c03\u7528glibc<\/p>\n<pre><code>D.Gen|icefire|2023-12-10 12:06:27[ice@ override-libc-new].\/main_not_touch<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"LD_PRELOAD%E5%95%A5%E9%83%BD%E4%B8%8Dlink\"><\/span>\nLD_PRELOAD+\u5565\u90fd\u4e0dlink<br \/>\n<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code>D.Gen|icefire|2023-12-10 12:06:55[ice@ override-libc-new]LD_PRELOAD=libmalloc.so .\/main_not_touch \nmy\n<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"%E5%85%88link_libc%E5%86%8Dlink_malloc\"><\/span>\n\u5148link libc\u518dlink malloc<br \/>\n<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code>.\/main_dynamic_glibc_first <\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"LD_PRELOAD%E5%9C%A8%E5%85%88link_libc%E5%86%8Dlink_malloc%E6%97%B6%E7%9A%84%E8%A1%A8%E7%8E%B0\"><\/span>\nLD_PRELOAD\u5728\u5148link libc\u518dlink malloc\u65f6\u7684\u8868\u73b0<br \/>\n<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u53d1\u73b0\uff0c\u7f16\u8bd1\u65f6: -lc -lmalloc\uff0c\u8fd0\u884c\u65f6LD_PRELOAD\uff0c\u6700\u7ec8\u8fd0\u884c\u65f6LD_PRELOAD\u7684\u4f18\u5148\u7ea7\u9ad8<\/p>\n<pre><code>D.Gen|icefire|2023-12-10 12:08:08[ice@ override-libc-new]LD_PRELOAD=libmalloc.so .\/main_dynamic_glibc_first \nmy<\/code><\/pre>\n<h1><span class=\"ez-toc-section\" id=\"%E7%9B%B8%E5%90%8C%E5%87%BD%E6%95%B0%E7%AD%BE%E5%90%8D%E7%9A%84%E5%A4%9A%E4%B8%AA%E5%AE%9E%E7%8E%B0%E4%B8%8D%E4%B8%80%E5%AE%9A%E4%BC%9A%E6%8A%A5multiple_definition%E7%9A%84%E9%94%99\"><\/span>\n\u76f8\u540c\u51fd\u6570\u7b7e\u540d\u7684\u591a\u4e2a\u5b9e\u73b0\u4e0d\u4e00\u5b9a\u4f1a\u62a5multiple definition\u7684\u9519<br \/>\n<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>main.c<\/p>\n<pre><code>\/\/ main.c\nextern void test();\nint main() {\n    test();\n}<\/code><\/pre>\n<p>test1.c<\/p>\n<pre><code>\/\/ test1.c\n#include &lt;stdio.h&gt;\n\nvoid test() {\n    printf(&quot;call from test1.c\\n&quot;);\n}<\/code><\/pre>\n<p>test2.c<\/p>\n<pre><code>#include &lt;stdio.h&gt;\n\nvoid test() {\n    printf(&quot;call from test2.c\\n&quot;);\n}<\/code><\/pre>\n<p>makefile<\/p>\n<pre><code>.PHONY:main clean\n%.o:%.c\n    gcc -c $&lt; -o $@\nmain:main.o test1.o test2.o\n    #gcc -o main main.o test1.o test2.o\n    ar rcs libtest1.a test1.o\n    ar rcs libtest2.a test2.o\n    gcc -fPIC -shared -o libtest1_dyn.so test1.o\n    gcc -fPIC -shared -o libtest2_dyn.so test2.o\n    gcc -L. -o main_static.elf main.o -ltest1 -ltest2\n    gcc -L. -o main_dyn1.elf main.o -ltest1_dyn -ltest2_dyn\n    gcc -L. -o main_dyn2.elf main.o -ltest2_dyn -ltest1_dyn \nclean:\n    rm -rf *.a *.so *.o<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8986\u76d6libc\u4e2d\u7684malloc, \u7c7b\u4f3chttps:\/\/dongbo.tech\/post\/2020\/overrid &#8230; <a title=\"\u8986\u76d6glibc\u4e2d\u7684\u51fd\u6570\u5b9e\u73b0\" class=\"read-more\" href=\"https:\/\/189505.xyz\/?p=329\" aria-label=\"More on \u8986\u76d6glibc\u4e2d\u7684\u51fd\u6570\u5b9e\u73b0\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/189505.xyz\/index.php?rest_route=\/wp\/v2\/posts\/329"}],"collection":[{"href":"https:\/\/189505.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/189505.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/189505.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/189505.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=329"}],"version-history":[{"count":12,"href":"https:\/\/189505.xyz\/index.php?rest_route=\/wp\/v2\/posts\/329\/revisions"}],"predecessor-version":[{"id":341,"href":"https:\/\/189505.xyz\/index.php?rest_route=\/wp\/v2\/posts\/329\/revisions\/341"}],"wp:attachment":[{"href":"https:\/\/189505.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/189505.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/189505.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}