[comp.sources.wanted] SUMMARY: C string to function_ptr routine.

jrknight@cs.arizona.edu (James R. Knight) (03/13/91)

I've had mail messages from people asking me to send what information
I got from this question to them, so I figured I'd also pass it on to
the newsgroup.


  From auspex!auspex.com!guy@uunet.UU.NET Thu Mar  7 17:12:58 1991
  Date: Thu, 7 Mar 91 15:47:33 PST
  From: guy@auspex.com (Guy Harris)
  Message-Id: <9103072347.AA25296@auspex.com>
  To: jrknight@cs.arizona.edu
  Subject: Re: Looking for C string to function_ptr routine.

  >I need a C routine which can retrieve the function pointer of a routine
  >in the executable from the string name of that function.  (Preferably
  >something smaller that the complete source code to dbx. :-))  

  Well, if you have a *non*-stripped version of the executable handy, look
  at the "nlist()" routine; it takes a pointer to the pathname of the
  executable, and a pointer to an array of symbol table entries with the
  symbol names filled in, and fills in the rest of those symbol table
  entries (assuming those symbols exist, of course), complete with the
  address corresponding to those symbols.

  Of course, there isn't necessarily a guarantee that you can convert
  those addresses into function pointers - consider some system where the
  first few bytes of a function contained stuff other than executable
  code, and where you're expected to jump N bytes after the symbol address
  to call a routine - but it should probably work on most UNIX systems.

  If you have only a stripped version of the executable handy, you lose.

  >Barring that, a reference which describes the format of the symbol table 
  >in a executable compiled under the -g flag would be a great help.

  The "-g" flag makes no difference to the symbols listed above; it
  generally just provokes the compiler into putting *additional* symbols
  into the symbol table for the benefit of the debugger.




  From twwells!frnkmth!frnk400!jesse@decwrl.dec.com Tue Mar 12 03:02:45 1991
  Date: Mon, 11 Mar 91 11:30:40 EST
  From: frnkmth!frnk400!jesse@decwrl.dec.com (Jesse Perry)
  To: twwells!cs.arizona.edu!jrknight@decwrl.dec.com
  Subject: C string to function pointer routine
  Cc: frnk400!jesse@decwrl.dec.com

  A few days ago you posted,

  > Article 3151 of comp.sources.wanted:
  > Subject: Looking for C string to function_ptr routine.
  >
  > I need a C routine which can retrieve the function pointer of a routine
  > in the executable from the string name of that function.  (Preferably
  > something smaller that the complete source code to dbx. :-))
  >
  > Barring that, a reference which describes the format of the symbol table
  > in a executable compiled under the -g flag would be a great help.

  I recently had to write something like this.  I used (and recommend
  to you) a method which may seem like overkill....

  I already had a set of routines to associate strings with values,
  and allow lookup by string -- an internal symbol table.  So to get
  the functionality you want, I did the following:

    1. My Unix program did a vfork() and exec() to run nm(1) on itself,
       sending the output to a temporary file.

    2. When the exec()ed nm(1) finished, my program loaded the temporary
       file into my own internal symbol table format, and deleted the
       temporary file.

    3. Thereafter, I could get the address of any function by its name,
       as you need to do.

  This method has the disadvantage that the very first time you need
  to do a look-up, it is slow, because it has to run an outside program.
  But thereafter, it is very fast, because the nm(1) doesn't have to
  be done again.

  It has the advantage that (a) you don't need any knowledge at all
  about the format of executable files, and (b) therefore, programs
  which use this method are quite portable (among Unix systems).

  Let me know if you need more information.

  Jesse Perry	jesse@franklin.com	{decwrl|uunet}!twwells!frnkmth!jesse



My thanks to these folks and Tim Jones for their help.

Jim