How to rename a C function name in its definition at runtime - c

Can anyone please solve the following problem for me:
Problem: Let say there are two functions foo() and bar() defined as
void bar()
{
printf("bar\n");
}
void foo()
{
printf("foo\n");
bar();
}
So, here I want to change the function name bar to bar_test in its definition but not in calling. This should be runtime and the source code should not be modified.
The expected output is as below:
void bar_test()
{
printf("bar\n");
}
void foo()
{
printf("foo\n");
bar();
}
Thanks

One way would be to have a macro like
#define bar() bar_test()
Now calling bar() by the macro calls bar_test()
The function which is defined should be called as per the standard.
There is no option to change the function name during runtime and it doesn't make sense also.

Related

Splitting main file into modules in C

I have three files as such:
module.c:
void bar() {
foo();
}
module.h: (i didn't put the include guard for simplicity)
void bar();
main.c
void foo() {
//some code
}
int main() {
bar();
}
when compiling main.c and module.c, module.c returns an error saying foo() is not defined. How can i fix this up?
Basically, i wanted to take my actual main file, which was pretty large, and split up parts of it to other files for readability, but those functions call other functions found in main
It's tricky because of the directions your dependencies are going.
I can split your code into three compilation units: main.c, module.c, and foo.c
If you do this, you don't have any foo code in main, main only calls bar, and bar includes foo, which is defined in foo.
main.c
#include "module.h"
int main() {
bar();
}
module.c
#include "foo.h"
void bar() {
foo();
}
foo.c
void foo() {
}
Best of all is that you don't need to declare foo outside of foo.h, or bar outside of module.h.
I guess module.h could also define void foo(); thus module.c would implement foo too. On the other hand, if bar depends indirectly on foo, then maybe module.c should include another_module.h implemented by another_module.c.
Put prototype definitions for all your functions into module.h:
int main(int argc,char **argv);
void foo(void);
void bar(void);
int fludger(int abc,char *str);
You get the side benefit that in addition to putting any function in any .c file you wish, you no longer need to order the functions within a given .c file, based upon what calls what [e.g. before if foo called bar, bar would have to be defined above foo]

Functions in C language

This code is working good.
#include<stdio.h>
#include<conio.h>
void show()
{
printf("Show function");
}
void main()
{
show();
}
but this requires some more.
#include<stdio.h>
#include<conio.h>
void main()
{
show();
}
void show()
{
printf("Show function");
}
That is.. I have to declare a "Show" function in main.
why i need to do this?
You need to declare show before main because main calls show before it is defined. You can do this with a function prototype as shown below
#include <stdio.h>
// You reference show in main but the compiler will see main before show
// This is required to let the compiler know show will later be defined
void show();
void main()
{
show();
}
void show()
{
printf("Show function");
}
In the first example, when C enounters the function call to show, it already knows what arguments the function takes, because the definition of show is above the call in the file, and C just goes thru once top to botom. In the second example the when call to show is hot C hasn't yet heard anything about a function named show.
Your code is parsed from beginning to end in one pass.
The second snippet won't compile since the compiler doesn't know what show () is once hitting the call.
As a fix, you can use a simple forward declaration, telling the compiler the function's name and signature without telling it about its body straight away:
#include <stdio.h>
#include <conio.h>
void show(); // Now the compiler knows the function
void main()
{
show();
}
void show()
{
printf("Show function");
}
Such declarations typically happen within header files. The actual implementation may then be out into any translation file (usually grouped as well).
You can declare the function before main like this (or inside a header file):
#include <stdio.h>
#include <conio.h>
void show();
void main() {
show();
}
void show() {
printf("Show function"
Actually you are not declaring the show() function in main but you are calling the show() function from main.
A function has to be declared outside of the main function or before calling it.
Function declaration is required when you define a function in one source file and you call that function in another file. In such case you should declare the function at the top of the file calling the function.
(Your program may run with a warning of function not declared)
#include <stdio.h>
void show();
void main()
{
show();
}
void show()
{
printf("Show function");
}
Because in genuine recent C every function has to be declared or defined (perhaps in some header file) before being used.
So add a line (declaring show) like
void show(void);
before your main in the second example. It is not needed in the first example because show is defined before its first use.
Notice that <conio.h> is not a standard header (don't exist on POSIX systems)

Fun with C macros

Let's say I have a function macro in C called FOO. There are also two macros called BAR1 and BAR2, which are basically two flavors of the same macro. I'd like to write a macro BAR such that it expands to BAR1 in functions which invoke FOO somewhere before the use of BAR and to BAR2 otherwise. So for example:
void func1(void)
{
FOO();
...
BAR();
}
would be equivalent to
void func1(void)
{
FOO();
...
BAR1();
}
while this function:
void func2(void)
{
BAR();
}
would be equivalent to
void func2(void)
{
BAR2();
}
I'd like to avoid introducing global variables or doing additional checks at runtime. Is this even possible?
Short answer: NO. The C precompiler knows nothing about function limits, so even if you managed to modify the BAR macro as you want it, that would not be limited to the current function anyway.
Now, if you are willing, you can add some checks to the BAR macro. And those checks can be written to be resolved at compile time, so no runtime overhead results.
For example:
extern char _sentinel_[2];
#define FOO() char _sentinel_;
#define BAR() if (sizeof(_sentinel_) == 1) BAR1() else BAR2()
The trick is that the look up of variable _sentinel_ will resolve the global variable or the local one, depending on the use of FOO(). And since the condition in the if is a compiler constant, the compiler will optimize out the other branch.
My attempted hack at using gotos failed because when FOO() isn't used, the jump label is missing for BAR(). But, fear not, I've come up with an even more gross hack.
You can use #includes instead of a macro for FOO() and BAR(). This will allow you absolute control on how the code gets expanded.
/* FOO file */
#define BAR_IS_BAR2
/* whatever code FOO needs to do */
/* BAR file */
#ifdef BAR_IS_BAR2
BAR2();
#undef BAR_IS_BAR2
#else
BAR1();
#endif
/*...in you source code...*/
void func1 () {
#include "FOO"
/*...*/
#include "BAR"
}
void func2 () {
#include "BAR"
}

Linux function redirect

Hey, I'm trying to wrap a function in my program without using LD_PRELOAD.
I have two functions with the same signature:
void someFunc () {
puts ("someFunc");
}
void someFunc_wrapper () {
puts ("someFunc_wrapper");
}
And I want to redirect any function call to someFunc with a call to the wrapper function.
Of course I could do that with defining macros or put the wrapper function into a shared library and then call the program like this:
LD_PRELOAD=./mylib.so my_program
However, I want to redirect the function calls at runtime, without modifying the program call.
As I understand, it should be possible to redirect these calls by modifying the executable's symbol table at runtime.
Any help will be appreciated :)
ld has the option --wrap=symbol that should do what you want. The manual page has an example how it works.
Even if it is doable, modifying your program's symbol table might not be enough in the general case - think about the compiler inlining your function.
You could do this without dirty tricks by using an indirect function call though (always call that function via a pointer-to-function, update that pointer when you want to switch implementations).
Note that this has some runtime overhead (one more indirection). Whether that's important or not depends on the use case. And it can be turned off at compile time with macro tricks if that's just a debug feature you're implementing.
#include <stdio.h>
void foo(void) {
printf("Hello from foo\n");
}
void bar(void) {
printf("Hello from bar\n");
}
#ifdef INTERCEPT
typedef void(*myfunptr)(void);
myfunptr thingy = foo;
void turnonbar(void)
{
thingy = bar;
}
#else
#define turnonbar()
#define thingy foo
#endif
int main(void) {
thingy();
turnonbar();
thingy();
return 0;
}

How to sort functions in C? "previous implicit declaration of a function was here" error

I'm sure this has been asked before, but I couldn't find anything that would help me.
I have a program with functions in C that looks like this
function2(){
function1()
}
function1 (){
function2()
}
main () {
function1()
}
It's more complicated than that, but I'm using recursion. And I cannot arrange the function in the file so that every function would only call functions that are specified above itself. I keep getting an error
main.c:193: error: conflicting types for 'function2'
main.c:127: error: previous implicit declaration of 'function2' was here
How do I avoid this? Thanks in advance for suggestions and answers.
You need to declare (not define) at least one function before using it.
function2(); /* declaration */
function1() { function2(); } /* definition */
function2() { function1(); } /* definition */
int main(void) { function1(); return 0; }
Foward declare your functions...
function1();
function2();
function2(){
function1()
}
function1 (){
function2()
}
main () {
function1()
}
Try:
function1();
function2();
function2(){
function1()
}
function1 (){
function2()
}
main () {
function1()
}
Forward declare your functions, but by using prototypes. If you have a lot of them such that you can't handle this, this is the moment to think of your design and to create a .h file with all your prototypes. Use
int function1(void);
int function2(void);
if that was what you meant. int function1() already is different from that in C. Help the compiler such that he can help you.
This is how C works. We need to declare the function before use. like when you use any variable, you must have declare first then you would have use it.
Declaration:-
function1();
function2();
and then put your own code.

Resources