RE: incorrect dereference of implicit memcpy

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



 
> Woytech Karl-Heinz wrote:
> When the application code below gets started our OS finds the 'imex'
> structure and correctly fills out the required function 
> pointers so that
> calls to the fn's result in the code provided by the os 
> getting called.
> 
> Thus the explicit call
>   jimbob(815);
> translates to 
>   push   $0x32f
>   call   *0x804912c

That's because it's not an explicit call to a function, but through a
function pointer which you declared.

> BUT the structure copy x=y becomes
>   push   $0x16
>   push   %eax
>   push   %edx
>   call   8049128  <- no '*'
> 
> which is a direct jump into memory instead of a jump to the 
> dereference

That's because the compiler does not read your memcpy declaration.
It assumes that there is a memcpy function and generates a call to it.
That generation is not influenced by your redeclaration of memcpy.

You must provide a real memcpy function.

> ie can i get gcc to produce the same derefecenced call as per the
> explicit calls?

Forget about it. 
 
> gcc testjmp.c -c -o testjmp.o -ffreestanding -march=i386 -g -Os
> -fno-builtin 
> ld testjmp.o -o testjmp -static -nostdlib
> objdump testjmp -DSx > testjmp.dis
> 
> testjmp.c:
> 
> // structure def for our import/export table
> typedef struct imex_s {
>   char name[16];
>   void *ptr;
>   } imex_t;
> 
> // fixed typedefs for our funcions
> typedef int  (*jimbob_t)(int jb);
> typedef void (*memcpy_t)(char *d, char *s, int l);

ANSI C memcpy is  void * (void *, const void *, size_t).

> // memory space for the to be linked fn's will be in .bss
> jimbob_t jimbob;
> memcpy_t memcpy;

Call these jimbob_ptr, memcpy_ptr.

> // our import table will be in .const
> const imex_t imex[] =
>  {
>    { "jimbob", (void*)&jimbob},

 &jimbob_ptr

>    { "memcpy", (void*)&memcpy}

 &memcpy_ptr

>  };

Now write this function and link it to your program:

  /* wrapper for indirection */
  void *memcpy(void *dest, void *src, size_t len)
  {
    memcpy_ptr(dest, src, len);
    return dest;
  }

Note that this wrapper not only ensures that control is routed
to your function, but also provides ANSI C compatibility.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux