{"id":4791,"date":"2014-03-30T15:22:13","date_gmt":"2014-03-30T15:22:13","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/accessing-the-code-section-of-a-c-program-collection-of-common-programming-errors\/"},"modified":"2014-03-30T15:22:13","modified_gmt":"2014-03-30T15:22:13","slug":"accessing-the-code-section-of-a-c-program-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/accessing-the-code-section-of-a-c-program-collection-of-common-programming-errors\/","title":{"rendered":"Accessing the code section of a C++ program-Collection of common programming errors"},"content":{"rendered":"<p>It is operating system specific; I am answering for Linux only.<\/p>\n<p>First your figure is grossly incorrect in practice, since most programs are dynamically linked to several shared object libraries (including <code>libc6.so<\/code>&#8230;). See also ld.so(8), elf(5), execve(2). There is no <em>single<\/em> <code>text<\/code> section (but many &#8220;text&#8221; like segments). Read about the pmap and objdump commands.<\/p>\n<p>Then, you can understand the address space of a process of pid 1234 with <code>cat \/proc\/1234\/maps<\/code> ; read more about proc(5)&#8230; From inside the program, read <code>\/proc\/self\/maps<\/code>; for instance try <code>cat \/proc\/$$\/maps<\/code> in a shell to show the address space of the shell process, and <code>cat \/proc\/self\/maps<\/code> for the address space of the process running that <code>cat<\/code> command. See also mmap(2).<\/p>\n<p>You cannot really &#8220;relocate&#8221; (you actually mean &#8220;move&#8221;) the text section. Some addresses are built inside the code. However, read about the -fPIE option of <code>gcc<\/code> (for <em>position independent executable<\/em> &#8230;.).<\/p>\n<p>Of course, you can access the symbols of a program (from inside) if you link it with the -rdynamic flag and if you use dlopen(3) (and <code>dlsym<\/code> and even perhaps <code>dladdr<\/code>&#8230;.) with a <code>NULL<\/code> first <code>filename<\/code> argument.<\/p>\n<p>See also wikipages about address space, virtual memory, ASLR, position independent code, relocation, ABI, JIT compilation, name mangling and the x86-64 ABI spec and the Advanced Linux Programming book.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is operating system specific; I am answering for Linux only. First your figure is grossly incorrect in practice, since most programs are dynamically linked to several shared object libraries (including libc6.so&#8230;). See also ld.so(8), elf(5), execve(2). There is no single text section (but many &#8220;text&#8221; like segments). Read about the pmap and objdump commands. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4791","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4791"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4791\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}