<p>How do I list the symbols being exported from a .so file?  If possible, I'd also like to know their source (e.g. if they are pulled in from a static library).</p>

<p>I'm using gcc 4.0.2, if that makes a difference.</p>

[{'answer_id': 34758, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 4, 'selected': False, 'text': "<p>You can use the <code>nm -g</code> tool from the binutils toolchain. However, their source is not always readily available. and I'm not actually even sure that this information can always be retrieved. Perhaps <code>objcopy</code> reveals further information.</p>\n\n<p>/EDIT: The tool's name is of course <code>nm</code>. The flag <code>-g</code> is used to show only exported symbols.</p>\n"}, {'answer_id': 34796, 'author': 'Steve Gury', 'author_id': 1578, 'author_profile': 'https://Stackoverflow.com/users/1578', 'pm_score': 10, 'selected': True, 'text': '<p>The standard tool for listing symbols is <code>nm</code>, you can use it simply like this:</p>\n\n<pre><code>nm -gD yourLib.so\n</code></pre>\n\n<p>If you want to see symbols of a C++ library, add the "-C" option which demangle the symbols (it\'s far more readable demangled).</p>\n\n<pre><code>nm -gDC yourLib.so\n</code></pre>\n\n<p>If your .so file is in elf format, you have two options:</p>\n\n<p>Either <code>objdump</code> (<code>-C</code> is also useful for demangling C++):</p>\n\n<pre><code>$ objdump -TC libz.so\n\nlibz.so:     file format elf64-x86-64\n\nDYNAMIC SYMBOL TABLE:\n0000000000002010 l    d  .init  0000000000000000              .init\n0000000000000000      DF *UND*  0000000000000000  GLIBC_2.2.5 free\n0000000000000000      DF *UND*  0000000000000000  GLIBC_2.2.5 __errno_location\n0000000000000000  w   D  *UND*  0000000000000000              _ITM_deregisterTMCloneTable\n</code></pre>\n\n<p>Or use <code>readelf</code>:</p>\n\n<pre><code>$ readelf -Ws libz.so\nSymbol table \'.dynsym\' contains 112 entries:\n   Num:    Value          Size Type    Bind   Vis      Ndx Name\n     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND\n     1: 0000000000002010     0 SECTION LOCAL  DEFAULT   10\n     2: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND free@GLIBC_2.2.5 (14)\n     3: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __errno_location@GLIBC_2.2.5 (14)\n     4: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTable\n</code></pre>\n'}, {'answer_id': 35328, 'author': 'Adam Mitz', 'author_id': 2574, 'author_profile': 'https://Stackoverflow.com/users/2574', 'pm_score': 4, 'selected': False, 'text': '<p>Try adding -l to the nm flags in order to get the source of each symbol.  If the library is compiled with debugging info (gcc -g) this should be the source file and line number.  As Konrad said, the object file / static library is probably unknown at this point.</p>\n'}, {'answer_id': 1620583, 'author': 'P Shved', 'author_id': 158676, 'author_profile': 'https://Stackoverflow.com/users/158676', 'pm_score': 7, 'selected': False, 'text': '<p>If your <code>.so</code> file is in elf format, you can use readelf program to extract symbol information from the binary.  This command will give you the symbol table:</p>\n\n<pre><code>readelf -Ws /usr/lib/libexample.so\n</code></pre>\n\n<p>You only should extract those that are defined in this <code>.so</code> file, not in the libraries referenced by it.  Seventh column should contain a number in this case.  You can extract it by using a simple regex:</p>\n\n<pre><code>readelf -Ws /usr/lib/libstdc++.so.6 | grep \'^\\([[:space:]]\\+[^[:space:]]\\+\\)\\{6\\}[[:space:]]\\+[[:digit:]]\\+\'\n</code></pre>\n\n<p>or, as proposed by <a href="https://stackoverflow.com/users/28817/caspin">Caspin</a>,:</p>\n\n<pre><code>readelf -Ws /usr/lib/libstdc++.so.6 | awk \'{print $8}\';\n</code></pre>\n'}, {'answer_id': 2073825, 'author': 'Pavel Lapin', 'author_id': 212229, 'author_profile': 'https://Stackoverflow.com/users/212229', 'pm_score': 6, 'selected': False, 'text': '<pre><code>objdump -TC /usr/lib/libexample.so\n</code></pre>\n'}, {'answer_id': 3657200, 'author': 'Peter Remmers', 'author_id': 255776, 'author_profile': 'https://Stackoverflow.com/users/255776', 'pm_score': 5, 'selected': False, 'text': '<p>I kept wondering why <em>-fvisibility=hidden</em> and <em>#pragma GCC visibility</em> did not seem to have any influence, as all the symbols were always visible with <em>nm</em> - until I found this post that pointed me to <em>readelf</em> and <em>objdump</em>, which made me realize that there seem to actually be <strong>two</strong> symbol tables:</p>\n\n<ul>\n<li>The one you can list with <em>nm</em></li>\n<li>The one you can list with <em>readelf</em> and <em>objdump</em></li>\n</ul>\n\n<p>I think the former contains debugging symbols that can be stripped with <em>strip</em> or the -s switch that you can give to the linker or the <em>install</em> command. And even if nm does not list anything anymore, your exported symbols are still exported because they are in the ELF "dynamic symbol table", which is the latter.</p>\n'}, {'answer_id': 4152074, 'author': 'zhaorufei', 'author_id': 64469, 'author_profile': 'https://Stackoverflow.com/users/64469', 'pm_score': 3, 'selected': False, 'text': "<p>nm -g  list the extern variable, which is not necessary exported symbol.\nAny non-static file scope variable(in C) are all extern variable.</p>\n\n<p>nm -D  will list the symbol in the dynamic table, which you can find it's address by dlsym.</p>\n\n<p>nm --version </p>\n\n<p>GNU nm 2.17.50.0.6-12.el5 20061020</p>\n"}, {'answer_id': 14737593, 'author': 'cavila', 'author_id': 936787, 'author_profile': 'https://Stackoverflow.com/users/936787', 'pm_score': 5, 'selected': False, 'text': '<p>For shared libraries libNAME.so the -D switch was necessary to see symbols in my Linux</p>\n\n<pre><code>nm -D libNAME.so\n</code></pre>\n\n<p>and for static library as reported by others</p>\n\n<pre><code>nm -g libNAME.a\n</code></pre>\n'}, {'answer_id': 26739013, 'author': 'Adi Shavit', 'author_id': 135862, 'author_profile': 'https://Stackoverflow.com/users/135862', 'pm_score': 4, 'selected': False, 'text': '<p>For Android <code>.so</code> files, the NDK toolchain comes with the required tools mentioned in the other answers: <code>readelf</code>, <code>objdump</code> and <code>nm</code>.</p>\n'}, {'answer_id': 48122967, 'author': 'user7610', 'author_id': 1047788, 'author_profile': 'https://Stackoverflow.com/users/1047788', 'pm_score': 5, 'selected': False, 'text': '<p>For C++ <code>.so</code> files, the ultimate <code>nm</code> command is <code>nm --demangle --dynamic --defined-only --extern-only &lt;my.so&gt;</code></p>\n\n<pre><code># nm --demangle --dynamic --defined-only --extern-only /usr/lib64/libqpid-proton-cpp.so | grep work | grep add\n0000000000049500 T proton::work_queue::add(proton::internal::v03::work)\n0000000000049580 T proton::work_queue::add(proton::void_function0&amp;)\n000000000002e7b0 W proton::work_queue::impl::add_void(proton::internal::v03::work)\n000000000002b1f0 T proton::container::impl::add_work_queue()\n000000000002dc50 T proton::container::impl::container_work_queue::add(proton::internal::v03::work)\n000000000002db60 T proton::container::impl::connection_work_queue::add(proton::internal::v03::work)\n</code></pre>\n\n<p>source: <a href="https://stackoverflow.com/a/43257338">https://stackoverflow.com/a/43257338</a></p>\n'}, {'answer_id': 54591492, 'author': 'Craig Ringer', 'author_id': 398670, 'author_profile': 'https://Stackoverflow.com/users/398670', 'pm_score': 2, 'selected': False, 'text': '<p>If you just want to know if there are symbols <em>present</em> you can use</p>\n\n<pre><code>objdump -h /path/to/object\n</code></pre>\n\n<p>or to list the debug info</p>\n\n<pre><code>objdump -g /path/to/object\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/34732', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3051/']