{"id":262,"date":"2025-11-22T13:31:42","date_gmt":"2025-11-22T13:31:42","guid":{"rendered":"https:\/\/haco.club\/?p=262"},"modified":"2025-11-22T13:32:50","modified_gmt":"2025-11-22T13:32:50","slug":"check-whether-an-executable-is-pure-c-or-cpp","status":"publish","type":"post","link":"https:\/\/haco.club\/?p=262","title":{"rendered":"Check whether an executable is pure C or CPP"},"content":{"rendered":"\n<p>Distinguishing between a pure C and a C++ executable can be achieved by examining the symbols and library dependencies of the binary file. C++ compilers employ a technique called &#8220;name mangling&#8221; to support function overloading and namespaces, which is absent in C. Furthermore, C++ programs have a distinct set of standard library dependencies.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Inspecting Symbol Tables for Name Mangling<\/h2>\n\n\n\n<p>A primary indicator of C++ code is the presence of &#8220;mangled&#8221; names in the executable&#8217;s symbol table. C++ compilers alter function and variable names to encode information about their types and namespaces, a process that C compilers do not perform.<\/p>\n\n\n\n<p><strong>On Linux and other Unix-like systems<\/strong>, you can use command-line tools like&nbsp;nm&nbsp;or&nbsp;objdump&nbsp;to view the symbol table.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Using&nbsp;nm<\/strong>: The&nbsp;nm&nbsp;utility lists the symbols from object files. A tell-tale sign of C++ is the presence of complex, decorated symbol names.codeBash<code>nm &lt;executable_name&gt; | grep ' T '<\/code>\n<ul class=\"wp-block-list\">\n<li><strong>Pure C symbols<\/strong>&nbsp;will typically look clean and straightforward, often with a leading underscore, like&nbsp;_main&nbsp;or&nbsp;_my_function.<\/li>\n\n\n\n<li><strong>C++ mangled symbols<\/strong>&nbsp;will be more elaborate. For instance, a function&nbsp;int MyClass::myFunction(int)&nbsp;might be mangled into something like&nbsp;_ZN7MyClass10myFunctionEi. The presence of such decorated names is a strong indication of C++ code.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Using&nbsp;objdump<\/strong>: Similar to&nbsp;nm,&nbsp;objdump&nbsp;can display the symbol table.codeBash<code>objdump -t &lt;executable_name&gt;<\/code>Look for function names that are not simple identifiers.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Inspecting library dependencies<\/h2>\n\n\n\n<p>In Linux, you can read the dependencies of an executable file using several command-line tools. These utilities analyze the executable and list the shared libraries it needs to run.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The&nbsp;ldd&nbsp;Command<\/h3>\n\n\n\n<p>The most common and straightforward tool for this purpose is&nbsp;ldd&nbsp;(List Dynamic Dependencies). It prints the shared libraries required by a program.<\/p>\n\n\n\n<p><strong>How to use&nbsp;ldd:<\/strong><\/p>\n\n\n\n<p>To check the dependencies of an executable, simply pass the path to the executable as an argument to the&nbsp;ldd&nbsp;command:codeBash<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ldd \/path\/to\/your\/executable<\/code><\/pre>\n\n\n\n<p>For example, to check the dependencies of the&nbsp;ls&nbsp;command, you would run:codeBash<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ldd \/bin\/ls<\/code><\/pre>\n\n\n\n<p><strong>Understanding the output of&nbsp;ldd:<\/strong><\/p>\n\n\n\n<p>The output of&nbsp;ldd&nbsp;is typically formatted into three columns:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Library Name:<\/strong>&nbsp;The first column shows the name of the shared library the executable depends on.<\/li>\n\n\n\n<li><strong>Path to the Library:<\/strong>&nbsp;The second column, following the&nbsp;=&gt;&nbsp;symbol, indicates the full path to where the system&#8217;s dynamic linker has found that library. If a library is not found, you will see a &#8220;not found&#8221; message.<\/li>\n\n\n\n<li><strong>Memory Address:<\/strong>&nbsp;The third column, in parentheses, displays the memory address where the library is loaded.<\/li>\n<\/ul>\n\n\n\n<p>Here is an example of the output for&nbsp;ldd \/bin\/bash&nbsp;and what it means:codeCode<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>linux-vdso.so.1 (0x00007ffc1b7d8000)\n    libtinfo.so.6 =&gt; \/lib\/x86_64-linux-gnu\/libtinfo.so.6 (0x00007f5b8d8a4000)\n    libdl.so.2 =&gt; \/lib\/x86_64-linux-gnu\/libdl.so.2 (0x00007f5b8d89e000)\n    libc.so.6 =&gt; \/lib\/x86_64-linux-gnu\/libc.so.6 (0x00007f5b8d6b8000)\n    \/lib64\/ld-linux-x86-64.so.2 (0x00007f5b8d90d000)<\/code><\/pre>\n\n\n\n<p>In this output:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>libtinfo.so.6&nbsp;is a required library.<\/li>\n\n\n\n<li>The system found it at&nbsp;\/lib\/x86_64-linux-gnu\/libtinfo.so.6.<\/li>\n\n\n\n<li>It is loaded at the memory address&nbsp;0x00007f5b8d8a4000.<\/li>\n<\/ul>\n\n\n\n<p>A special entry,&nbsp;linux-vdso.so.1, is a virtual dynamic shared object provided by the kernel to accelerate certain system calls and is not a file on disk. The&nbsp;\/lib64\/ld-linux-x86-64.so.2&nbsp;is the dynamic linker itself, which is responsible for loading the other shared libraries.<\/p>\n\n\n\n<p><strong>Important Security Note:<\/strong>&nbsp;You should never run&nbsp;ldd&nbsp;on an untrusted executable. In some cases,&nbsp;ldd&nbsp;might directly execute the program to determine its dependencies, which could be a security risk.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Alternative Tools<\/h3>\n\n\n\n<p>While&nbsp;ldd&nbsp;is the most common tool, there are other utilities that can provide dependency information, sometimes with more detail or in a safer manner for untrusted files.<\/p>\n\n\n\n<p><strong>readelf:<\/strong>&nbsp;The&nbsp;readelf&nbsp;command can display information from ELF (Executable and Linkable Format) files, which is the standard binary file format on Linux. To see the list of needed shared libraries, you can use the&nbsp;-d&nbsp;or&nbsp;&#8211;dynamic&nbsp;option and filter for &#8220;NEEDED&#8221;.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>readelf -d \/path\/to\/your\/executable | grep 'NEEDED'<\/code><\/pre>\n\n\n\n<p>This method is safer than&nbsp;ldd&nbsp;for untrusted executables because it only reads the file&#8217;s metadata and does not execute any code.<\/p>\n\n\n\n<p><strong>objdump:<\/strong>&nbsp;Another versatile tool for inspecting binary files is&nbsp;objdump. While it doesn&#8217;t have a direct option to list dependencies in the same way as&nbsp;ldd, it can be used to examine the dynamic linking information within the executable. It is generally more complex for this specific task.<\/p>\n\n\n\n<p><strong>lddtree:<\/strong>&nbsp;For a more comprehensive, hierarchical view of dependencies (including the dependencies of the dependencies), you can use&nbsp;lddtree&nbsp;(also known as&nbsp;pax-utils&nbsp;on some distributions).This tool presents the information in an easy-to-understand tree format.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lddtree \/path\/to\/your\/executable<\/code><\/pre>\n\n\n\n<p>By using these tools, you can effectively inspect and understand the library dependencies of any executable on a Linux system.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Distinguishing between a pure C and a C++ executable can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[30,45,46],"class_list":["post-262","post","type-post","status-publish","format-standard","hentry","category-tutotial","tag-binary","tag-c","tag-cpp"],"_links":{"self":[{"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts\/262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=262"}],"version-history":[{"count":2,"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts\/262\/revisions"}],"predecessor-version":[{"id":264,"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts\/262\/revisions\/264"}],"wp:attachment":[{"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}