Assuming that by saying a binary you mean an executable file, you generally cannot load symbols from an executable on linux1 - the C++ code will have to be written or refactored as a (shared) library. Then you will be able to call dlopen
and dlsym
to load the relevant symbol.
The question that remains is how do you locate the symbols? When you move away from assembly to C++, and your question is tagged as such, you relinquish most of the control over the symbols' encoding as assembly labels to the compiler and the linker. For example, you will have to consider things such as C++ name mangling. The best approach to enumerate names is to invoke nm
to list all the symbols and parse the output to extract the symbol names you are interested in - to my knowledge there is no linux API that nm
calls that you can also call yourself. There is too much specific file format knowledge you will have to account for otherwise, to enumerate the symbols across the different versions of linkers and executables on linux.
EDIT. You indicated that nm
is not an option. Given this restriction, I think that you will have to resort to manually maintaining the list of symbols. Otherwise you are looking at duplicating what essentially do_lookup
does in glibc as it walks the link map.
Additionally note that you will probably have to mark your symbols with extern "C"
to avoid name mangling, and to be able to sanely maintain the discussed list of symbols.
1 Apart from the special case of a PIE executable.