27 Sep, 2008, Lobotomy wrote in the 1st comment:
Votes: 0
I'm trying to locate some sort of function or system accessible to C for iterating through all of the function pointers and whatnot in a program's executable (mine is already set up to use dlsym, dladdr, etc). I was hopeful that dl_iterate_phdr would be what I was looking for, but that appears to only work on shared library objects (such as libgcc_s.so, etc). If anyone knows a potential solution or can offer some help and/or insight on the matter, I'd appreciate it.

Although, I suspect that it might not be possible with C at all. :sad:
27 Sep, 2008, David Haley wrote in the 2nd comment:
Votes: 0
I'm not sure. What exactly are you going to do with them as you iterate over them? You won't/can't have the function types, so you won't really be able to call arbitrary functions. And you'll only have function names if the compiler generated debugging information.
27 Sep, 2008, Lobotomy wrote in the 3rd comment:
Votes: 0
The function names (and pointers as a result) are all I need, actually. At the moment I was looking to use the information to build a function scanner for my codebases's dynamic command system that would, during loadup, search for function names prefixed with a particular string that designates them as commands which are not yet present in a particular command table and then load them into it automatically. I.e, say a function were to begin with "command_(etc)" and it were not found belonging to any command in a particular command table, then it would be added into it. It's not really that important, as it merely eliminates the single step of having to enter new commands manually with an command editor command of some sort, but it would be interesting and useful to have. :thinking:
27 Sep, 2008, David Haley wrote in the 4th comment:
Votes: 0
Well, if you figure out how, I'd be curious… Personally, though, I wouldn't spend too much time on this, because I think the step of making the command is a lot less time-expensive than figuring out function introspection in C. :wink:
27 Sep, 2008, Lobotomy wrote in the 5th comment:
Votes: 0
Introspection, huh? Wasn't aware of there being a term to go along with it. If I do figure it out I will let you know, although I was somewhat hopeful that by your response you had an idea of how to do it. :sad:

I do have a slight idea, in that if the functions in question were accessible as shared objects they could be accessed directly (in theory, anyways) by the dl_iterate_phdr function. However, from the little bit I've gleaned regarding the creation of shared objects it seems like it would be entirely too cumbersome to be practical; unless I'm misunderstanding it, anyways. I'd like to find a method that's straightforward, much like the dlsym and dladdr functions (which I've turned into str_to_ptr() and ptr_to_str() functions, respectively).

While I'm at it, I do have another idea somewhat in that it might be possible to create a seperate program or sub-function of the main program that searches through the actual source files for function declarations, compiles a list of the results, and then feeds them into a function that runs the names one by one through dlsym() to try and find results, but aside from it possibly not working, it's not as elegant or as straightforward of a method as I would like. Thoughts?

Edit: The site I used as information regarding dl_iterate_phdr is here.
27 Sep, 2008, David Haley wrote in the 6th comment:
Votes: 0
Yup, introspection is what this is called: looking at your available methods/functions/etc.

The problem is that only shared objects have any kind of export table. In normal objects, especially without debugging information turned on, the functions are just addresses in the object file or executable. In order to iterate over them, you need some kind of table of functions, which most (if not all?) compilers do not provide.

The idea of a separate program that gets function prototypes and then having dlsym try each one could work, if that's really what you want to do. It actually seems a lot more straightforward than some of the other ideas, but I agree that it's not terribly elegant.

Registering the functions one way or another is a pretty good approach. It's what I normally do when I want to have a list of 'things' (objects, classes, functions, …) without having to do it explicitly. It's not completely automatic, so I hadn't suggested it earlier.
27 Sep, 2008, Davion wrote in the 7th comment:
Votes: 0
Lobotomy said:
While I'm at it, I do have another idea somewhat in that it might be possible to create a seperate program or sub-function of the main program that searches through the actual source files for function declarations, compiles a list of the results, and then feeds them into a function that runs the names one by one through dlsym() to try and find results, but aside from it possibly not working, it's not as elegant or as straightforward of a method as I would like. Thoughts?


I've used ctags before to accomplish this. Run ctags on a file, grab appropriate information, parse, execute. You most likely wont even need a separate program to do it.
27 Sep, 2008, Lobotomy wrote in the 8th comment:
Votes: 0
Davion said:
I've used ctags before to accomplish this. Run ctags on a file, grab appropriate information, parse, execute. You most likely wont even need a separate program to do it.

Outstanding! Ctags will save me a considerable amount of time. Thank you. :smile:

Edit: Works like a charm. :smile:
0.0/8