0

Suppose I have a small program like this one:

int i = 0;
int *pi = &i;
int **ppi = π

int main(int argc, char *argv[]) {
  return i + *pi + **ppi;
}

Is there a way to intercept the initialization of pi and ppi when they are set during program load/setup? I'd like to hook into their initialization so that I may rewrite the pointer values and stick a few of my own bits in, if possible before main() runs. All this should be transparent and automated.

I have looked into the LD Audit interface (link) but that provided only callbacks for functions.

Jens
  • 8,423
  • 9
  • 58
  • 78
  • 3
    In some environments the globals are allocated as a part of the program bytes and already initialized in the load image. – Hot Licks Mar 05 '14 at 03:33
  • 2
    What are you really trying to do? (It might help provide an answer.) – TypeIA Mar 05 '14 at 03:35
  • @dvnrrs: Edited the question: I'd like to catch when they are initialized so I can stick a few custom bits into the pointer values. – Jens Mar 05 '14 at 03:42
  • What's wrong with doing it at the beginning of `main`? – sfstewman Mar 05 '14 at 03:43
  • If you are using `gcc`, then you can. Check [this](http://stackoverflow.com/a/10897934/1004301) out. – Lee Duhem Mar 05 '14 at 03:46
  • @sfstewman: because when I link multiple object files then I don't really know what globals they will contribute. And for large numbers of global data a more automated and transparent rewrite is more feasible. – Jens Mar 05 '14 at 03:48
  • @leeduhem: this is similar to sfstewman's question, see my answer. While such a constructor allows me to execute some code before main() I would still have to implement all the rewrite code by hand, and it would be incomplete. – Jens Mar 05 '14 at 03:52
  • I don't understand what you mean about "inserting some bits." If you do that to a pointer it becomes an invalid, unusable pointer. What am I missing? – TypeIA Mar 05 '14 at 03:57
  • @dvnrrs: That is intentional, and you can assume that using such a modified pointer is safe. – Jens Mar 05 '14 at 03:58
  • @Jens Sorry, I misread your question. – Lee Duhem Mar 05 '14 at 04:28
  • You're asking for compile-time or runtime reflection. There's no way to do it in standard C. You may be able to do it on a particular implementation. You haven't specified a toolchain or platform, so we have to assume a standard dialect and a compliant implementation. The C standards provide no way to do what you seem to want. – sfstewman Mar 05 '14 at 06:23
  • @sfstewman: I was hoping for an API similar to the LD auditing, which is pretty standard. Of course, if there is no such "open door" then I'll have to resort to surgery of a compiler (LLVM, quite likely). – Jens Mar 05 '14 at 06:41

2 Answers2

2

This is an implementation detail, not part of the language. There might be a way to do it, but then you'd no longer be writing in C.

And why would you want to? If you need to do something before main(), why not just convert something like this:

int main(int argc, char *argv[]) {
    // Do stuff
}

to something like:

int old_main(int argc, char *argv[]) {
    // Do stuff
}

int main(int argc, char *argv[]) {
    // Do earlier stuff
    return old_main(argc, argv);
}

And why bother intercepting initializers? Just let the values be initialized, then change them to what you want.

Any time you try to work around the language instead of working with it, you're asking for bugs and unpredictable behavior.

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
  • I'd be happy to change the pointers after their initialization. The challenge is to find all the global pointers, what they point at, and change them accordingly. I figure that somewhere along the way to `main()` the pointers are initialized, and that would be the easiest and most generic way to catch them _all_. – Jens Mar 05 '14 at 03:57
  • If by "find" you mean "find them by name", then that's called runtime introspection, which C doesn't have. Variable names only exist at compile time. If you don't already know where they are, there's no way to find them later. – Lee Daniel Crocker Mar 05 '14 at 04:02
  • Not by name. In the above example I declare two pointers, and at some point there are initialized to point at some allocated object. I'm looking for a way to intercept (or change at some other point before `main()`) when these pointer values are set so that once `main()` runs whoever loads `pi` or `ppi` sees the modified pointers. – Jens Mar 05 '14 at 04:05
  • So just do it the way I suggested...change them in the real `main()`, and do whatever you were doing there in a `old_main()`. – Lee Daniel Crocker Mar 05 '14 at 04:06
  • That assumes I know all globals that end up in the linked and executable binary, which I quite likely don't. – Jens Mar 05 '14 at 04:13
  • The globals in code that isn't yours you have no business messing with, and if you don't know the globals in your own code, then you're really in trouble. I don't understand what you're trying to accomplish here, but whatever it is, it's outside the language. – Lee Daniel Crocker Mar 05 '14 at 06:39
0

All c program will be compiled and linked by the compiler such as GCC. Each compiler may give you these features such as function invoke hooks and so on.

As I know the Function invoke analysis can use these features. If you know the chinese , you can look the detail of this document.

But I do not find a way to hook the global variables. These variables can be initialized by the compiler in linking data-area step.

QJGui
  • 907
  • 8
  • 10