LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Blogs > rainbowsally
User Name
Password

Notices


Rate this Entry

Simple C: some ideas on how to find headers, names, and a name demangler tool

Posted 02-08-2012 at 03:23 AM by rainbowsally
Updated 08-06-2014 at 10:30 AM by rainbowsally

The simplest yet, I think.. at least the C program is.

Features:
  • tips on how to find C/C++ header paths and declarations
  • tips on how to find libs containing references and definitions
  • a super-simple program to demangle C++ names
----------------
Note: With even a simple automatic makefile generator such as the one built from sources in this blog (
http://www.linuxquestions.org/questi...-part-2-34421/),
you can edit the makefile with a text editor to compile libs (an example coming up very soon), static/dynamic, change from 32-bit to 64-bit executables, add and remove files, copy headers, and/or binaries for installation, with a single command (such as 'makefile-creator <outputname>'.

So using makefiles is HIGHLY recommended especially if you are experimenting with C or C++ or developing applications where the files may change a lot.

And if you want a more full-featured makefile generator, check out AutoBake -- new and improved but still obviously the work of a backyard mechanic. :-)

http://rainbowsally.org/rainbowsally...2-02-04.tar.gz

The makefile-creator posted here (the first link near the top) will do the job nicely for this and other simple apps. And boy is this one SIMPLE! :-)
---------
Now let's see what's up in today's Computer Mad Science or whatever. ;-)

If you can't find a header or want to look at it in an editor...
Code:
find <GUESS_BRANCH> -name <QUOTED_NAME>
where GUESS_BRANCH is something like '/usr/include/*' or
'/usr/include/*/*' (low level system dependent headers).

If you need to find a library where a certain function is defined it can be found a couple of ways.

first check the development libs in /usr/lib
Code:
grep demangle /usr/lib/*.so 
grep demangle /usr/lib/*.a
then the development libs in /lib
Code:
grep demangle /lib/*.so 
grep demangle /lib/*.a
---------------
Note: If you suspect there is no development lib, add a splat after the *.so name so you can also check dso's with numeric extensions but be aware that they can't be linked with the -l switch in the Makefile LDFLAGS. They need to be listed by their full paths.
---------------

Here's what I get looking for 'demangle', for example, which we'll need for our demangling application that we create below.

Code:
grep demangle /usr/lib/*.so
  Binary file /usr/lib/libstdc++.so matches

grep demangle /usr/lib/*.a
  Binary file /usr/lib/libiberty.a matches
  Binary file /usr/lib/libstdc++.a matches
  Binary file /usr/lib/libsupc++.a matches

grep demangle /lib/*.so
  no hits

grep demangle /lib/*.so
  no hits
The best guess for a definition of this function would be libiberty.a for several reasons. One is that all the others are similar types of libs and probably have a common set of functions defined from somewhere and the oddball is libiberty, so that's most likely the common set of definitions.

We can verify this hunch rather than experimenting with the LDFLAGS in the makefile (easy enough to do but this is quicker) like this:
Code:
# Compare objdump of static and dynamic symbols:
  objdump -tT /usr/lib/libstdc++.so | sed '/demangle/!d'
  objdump -tT /usr/lib/libiberty.a | sed '/demangle/!d'
Here's what some of the symbols mean in these dumps.
Code:
Legend:
  g = 'global'
  F = 'function'
  O = 'object' (or data/struct, etc.)
  .text = executable code section, read only
  .data = initialized data, read-write
  .bss  = uninitialized data, 
  *UND* = undefined, requires definition from elsewhere
  [etc.]
libiberty is where our function "cplus_demangle_v3" is defined so that is the lib we'll link in a little program to demangle C++ names.

Here's the program:
Code:
// file: main.cpp 
// executable: demangle
#include <stdio.h>    // FILE stuff, printf(), sprintf(), etc.
#include <malloc.h>   // memory allocation
#include <string.h>   // strings and block comparisons and moves
#include <demangle.h> // the main function

int main(int argc, char** argv)
{
  // TODO: a more meaningful error handler
  if(argc < 2)
    exit(1); 
    
  // extern char * cplus_demangle (const char *mangled, int options);
  int flags = DMGL_PARAMS | DMGL_VERBOSE | DMGL_TYPES;
  char* s = cplus_demangle_v3(argv[1], flags);
  if(s)
    printf("%s\n", s);
  else
    printf("%s(...)\n", argv[1]); // or write a "can't demangle" warning.
  return 0;
}
If you use the makefile-creator utility from sources posted in this blog http://www.linuxquestions.org/questi...-part-2-34421/
create the makefile with the C++ option [you have added your own code to remind you of the syntax and do some error checks by now, right? :-) ] and add '-liberty' to the LDFLAGS with a text editor

Using this 'demangle' utility once it's built, here's what we get for a few of the mangled names in /usr/lib/libstdc++.so
Code:
  # objdump sees: 000d3db0  w   DO .data 00000008  CXXABI_1.3  _ZTIw
  $> demangle _ZTIw
  typeinfo for wchar_t
  
  # And I have always wondered what a "C1ERKSs" was, haven't you?
  $> demangle _ZNSt14overflow_errorC1ERKSs
  std::overflow_error::overflow_error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
  # and now I regret having asked. :-)
Enjoy! :-)
Posted in Uncategorized
Views 1429 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 02:05 AM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration