How do I insert a marker in c code so that it appears in the .elf file?
Well, I suppose you could just insert a literal string into your code. If you don't actually use that line, the compiler might decide to remove it during optimization, so maybe you can try something like this:
#include <stdio.h>
int main(int argc, char *argv[]) {
char *id_string = "MARKER";
if (argc < 0) { // (never true)
puts(id_string);
}
return 0;
}
source to share
Why complicate things? The way to insert things into ELF binaries is to declare global variables and non-static functions. There is no need to uncontrollably dig into binary with strings and other tools like this, or dig up magic numbers that might have been accidentally created by someone.
int this_is_my_marker;
The symbol will be displayed in the symbol table this_is_my_marker
.
This assumes you are on a relatively sane system with dynamic link libraries and unixy semantics (which is almost implied by ELF). The reason why all globals / functions end up in the symbol table by default is because it is allowed for dynamic libraries to resolve symbols from the main program. Since this has been done for almost every case, there is a small risk that someone will invent a unixy operating system where it is not. (which is why gcc has these weird flags -fvisibility
)
source to share