Skip to content
Snippets Groups Projects
Commit e44ab316 authored by Jake Feddersen's avatar Jake Feddersen
Browse files

Notes and updated gitignore

parent 44f2e15f
No related branches found
No related tags found
No related merge requests found
*.o
*.d
rlg327
battleship
File added
#include <stdio.h>
#include <stdarg.h>
__attribute__((constructor))
void init(void) {
//init the library
printf("About to replace printf.\n");
}
__attribute__((destructor))
void destroy(void) {
printf("All done.\n");
}
int myprintf(const char *format, ...) {
int ret;
va_list ap;
printf("You are inside my nefarious printf!\n");
va_start(ap, format);
ret = vprintf(format, ap);
va_end(ap);
printf("Okay. Now leaving.\n");
return ret;
}
File added
#include <stdio.h>
#include <dlfcn.h>
int main(int argc, char *argv[]) {
void *handle;
int (*printf)(const char *, ...);
if (!(handle = dlopen("./libmyprintf.so", RTLD_LAZY))) {
perror("dlopen");
return -1;
}
if (!(printf = dlsym(handle, "myprintf"))) {
perror("dlsym");
return -1;
}
printf("Hello World!\n");
dlclose(handle);
return 0;
}
Static library
- required code is inserted into binary at compile time
Dynamic library
- loaded at runtime, and only functionality needed by program is pulled
in on demand
Compiling Static Library
ar -cvq library.a file1.o file2.o
Compiling Dynamic Library
gcc with some .so output
Static library is self-contained; dynamic library may implicitly depend on
other libraries
For our example DLL:
gcc -fPIC -Wall myprintf.c -c
gcc -shared -o libmyprintf.so myprintf.o
gcc myprintf_test.c -o myprintf_test -l myprintf -L. -ldl
Electric Fence -- malloc debugging tool
Uses dynamic linking to replace malloc, related functions
Allocates memory around each request and verifies it doesn't try to access
Better tools in valgrind, gdb, so electric fence not used much any more
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment