A propos de l'option --wrap du linker gnu
Bonjour,
Je me pose certaines questions à propos de l'option --wrap du linker gnu permettant de créer automatiquement un alias sur un certain symbole.
Code:
1 2
| --wrap symbol
Use a wrapper function for symbol. Any undefined reference to symbol will be resolved to __wrap_symbol. Any undefined reference to __real_symbol will be resolved to symbol. This can be used to provide a wrapper for a system function. The wrapper function should be called __wrap_symbol. If it wishes to call the system function, it should call __real_symbol |
J'ai tout d'abord essayé cette option comme indiqué dans l'exemple. A savoir, j'ai écrit le code suivant:
Code:
1 2 3 4 5 6 7 8 9 10
| //essaiWrap.c
#include "malloc_wrapper.h"
int main(void)
{
int* a = (int *) malloc(sizeof(int));
*a = 3;
printf("a = %i", *a);
free(a);
} |
Code:
1 2 3
| //malloc_wrapper.h
#include <stdlib.h>
void * __wrap_malloc(size_t c); |
Code:
1 2 3 4 5 6 7 8 9
|
//malloc_wrapper.c
#include "malloc_wrapper.h"
void * __wrap_malloc(size_t c)
{
printf ("malloc called with %i\n", c);
return ((void*)__real_malloc(c));
} |
Je compile ce code avec le makefile suivant :
Code:
1 2 3 4 5 6 7 8 9 10
| all : essaiWrap
essaiWrap : essaiWrap.o malloc_wrapper.o
mingw32-gcc.exe -o essaiWrap.exe -Wl,--wrap,malloc essaiWrap.o malloc_wrapper.o
malloc_wrapper.o : malloc_wrapper.c
mingw32-gcc.exe -c -I. malloc_wrapper.c
essaiWrap.o : essaiWrap.c
mingw32-gcc.exe -c -I. essaiWrap.c |
la compilation se passe bien, mais à l'execution j'ai :
malloc called with 60
malloc called with 4
a = 3
Première question : d'où vient le premier malloc?
Ensuite, j'essaye de mettre la définition du __wrap_malloc dans une librarie.
malloc_wrapper.c devient donc :
Code:
1 2 3 4 5 6 7 8 9
|
//malloc_wrapper.c
#include "malloc_wrapper.h"
void * __wrap_malloc(size_t c)
{
printf ("malloc called with %i\n", c);
return ((void*)malloc(c)); //plus d'appel à __real_malloc car la librarie sera créee sans l'option --wrap
} |
Le reste du code est identique, seulement le makefile est maintenant le suivant:
Code:
1 2 3 4 5 6 7 8 9 10
| all : essaiWrap
essaiWrap : essaiWrap.o libmalloc_wrapper.a
mingw32-gcc.exe -o essaiWrap.exe -Wl,--wrap,malloc -L. -lmalloc_wrapper essaiWrap.c
libmalloc_wrapper.a : malloc_wrapper.o
ar -cru ./libmalloc_wrapper.a malloc_wrapper.o
malloc_wrapper.o : malloc_wrapper.c
mingw32-gcc.exe -c -I. malloc_wrapper.c |
Avec ce makefile, je ne peux plus compiler (le compilateur me dit que le symbole __wrap_malloc n'est pas défini...). Quelqu'un voit-il d'où vient le problème?
Enfin dernière question, j'ai fait quelques recherches mais je n'ai pas trouvé d'équivalent à cette option sur le linker du compilo visual. Est ce qu'il n'y en a effectivement pas? Si c'est le cas quelqu'un sait-il à quoi celà est-il du?
Merci de m'avoir lu,
Zillon