Question

Target

I want to resolve the signatures of all symbols linked to a specific section of my binary (not of a shared library).

What I've done

In assembly, you can define a section .example (NASM syntax) and then link it to a specific location in my binary using:

exampleStart = .;
*(.example)
exampleEnd = .;

This allows me to iterate over all symbols using the exampleStart and exampleEnd, as long as I know what they are. For example, if I know that all symbols are methods which require no parameters and return nothing, I can cast them to void(*)() and call them as methods.

Questions

  1. Is there a way to define sections in C++, similar to the .section syntax in NASM?
  2. Can I tell the linker to create something like a symbol table which additionally contains the signatures of the methods?

An example

In the following I want to show a little example of what I want to achieve (pseudo-C++):

SECTION example

void somePrefix_myFunction() {
    // ...
}

int somePrefix_anotherFunction(int a, int b) {
    return a + b;
}

ENDSECTION

Somewhere else I then want to be able to iterate over the symbols in this section and obtain the location and the signature of the symbol.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top