2 ms·
You can just append a function with __attribute__((constructor)) if GNU extensions are allowed and that will run before main and let you mess with the function
by Y_Y 2y ago
You can just append a function with __attribute__((constructor)) if GNU extensions are allowed and that will run before main and let you mess with the function pointers.
- kragen 2y agoIn an undefined order?
- Y_Y 2y agoObjection! Badgering the witness! I'm sure you know, but the undefined order is easily fixed by e.g. keeping a priority queue for your appended functions and then each time you add one you can include a constructor that enters it in the queue with a priority corresponding to its desired position.
- kragen 2y agoAnd then main() pops the latest override off the priority queue? I guess you're right; GCC extensions do solve the problem. And you can do the same thing with static object constructors in C++, though language-lawyeringly I'm not sure the standard guarantees enough about static initialization order to give the first constructor a well-defined empty queue.