This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Fwd: error in variable dereferencing
On 4/21/06, Andrew Haley <aph@gcc.gnu.org> wrote:
> Thibaud GUERIN writes:
> > >
> > > Now, if you know absolutely for sure that your args are pushed onto
> > > the stack in order without any holes, you might be able to get around
> > > this.
> > >
> > > void pkludge (char *s, ...) __attribute__((noinline));
> > > void pkludge (char *s, ...)
> > > {
> > > void **p = &s;
> > > printf ("%d\n", *(int *)++p);
> > > printf ("%d\n", *(int *)++p);
> > > }
> > >
> > > This isn't legal C, though, and trying to do things like this behind
> > > gcc's back is asking for trouble. The _real_ solution is to find out
> > > why gcc's builtins are not working.
> > >
> >
> > Thanks for all the tips (maybe REALLY usefull later).
> >
> > But my problem right now is that :
> >
> > int my_own_printf(char *fmt, ...)
> > {
> > char **s = (char **)(&fmt);
> >
> > if (*s == fmt)
> > write(1, "they are the same\n", 18);
> > else
> > write(1, "they aren't the same\n", 21);
> > return (0);
> > }
> >
> > prints "they aren't the same" ....
>
> OK, so we've got rid of the asms and casting away const, and the code
> still doesn't work.
>
> So, your code is now, at last, legal C. Let's have a look at the
> assembly code generated for my_own_printf (use gcc -S). Be sure to
> include the exact command line you used.
>
(my_own_printf is in the test_suites.c) :
gcc -c -o test_suites.o test_suites.c -std=gnu99 -Wall -nostdinc
-Wstrict-aliasing=2 -S -fno-builtin -I../include -I../
give me :
(snip)
.LC0:
.string "they are the same\n"
.LC1:
.string "they aren't the same\n"
.text
.globl my_own_printf
.type my_own_printf, @function
my_own_printf:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
leal 8(%ebp), %eax
movl %eax, -4(%ebp)
movl -4(%ebp), %eax
movl (%eax), %eax
cmpl 8(%ebp), %eax
jne .L33
subl $4, %esp
pushl $18
pushl $.LC0
pushl $1
call write
addl $16, %esp
jmp .L34
.L33:
subl $4, %esp
pushl $21
pushl $.LC1
pushl $1
call write
addl $16, %esp
.L34:
movl $0, %eax
leave
ret
.size my_own_printf, .-my_own_printf
.section .rodata
.LC2:
.string "string2"
.LC3:
.string "string1"
.text
(snip)
--
Thibaud