How do I insert a marker in c code so that it appears in the .elf file?

I want to check if the .elf file was generated from specific code or from other code

Is there a way to write something in C so that it can be identified by looking at the binary elf file?

+3


source to share


4 answers


Found a solution that works for me:



static const char MARKER[] __attribute__((used)) = "A_A_A_A";

      

+1


source


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;
}

      

+4


source


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

)

+2


source


You can use operator db

(or .byte

).

__asm {
    db 0x00 0x01 0x02
}

      

Don't forget to jump over this piece of code.

+1


source







All Articles