Skip to content

Instantly share code, notes, and snippets.

@sw17ch
Last active December 10, 2015 13:08
Show Gist options
  • Save sw17ch/4438798 to your computer and use it in GitHub Desktop.
Save sw17ch/4438798 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include "foo.h"
void foo(void) {
printf("foo!\n");
}
#ifndef FOO_H
#define FOO_H
void foo(void);
#endif /* FOO_H */
#include <stdio.h>
#include "foo.h"
void foo(void) {
printf("shim!\n");
old_foo();
}
#include <stdio.h>
#include "foo.h"
int main(int argc, char * argv[]) {
foo();
return 0;
}
main: main.c shim.o
shim.o: foo.c foo_shim.c foo.h
#
# Compile Files
gcc foo.c -c -o foo.o
gcc foo_shim.c -c -o foo_shim.o
#
# Rename the symbol we care about: foo -> old_foo
objcopy --redefine-sym foo=old_foo foo.o _foo.o
#
# Link foo_shim, define shim_foo as foo
ld foo_shim.o -r -defsym shim_foo=foo -o _foo_shim.o
#
# Smush it all together
ld _foo.o _foo_shim.o -r -o shim.o
clean:
rm -f main
rm -f *.o
@sw17ch
Copy link
Author

sw17ch commented Jan 2, 2013

Output:

$ ./main
shim!
foo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment