Detecting wrong linkage on mips PIC code

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Detecting wrong linkage on mips PIC code

Atsushi Nemoto
This simple queer program does not work on mips-linux if compiled with
-O2 option.  It works if compiled with -O.  I'm using gcc 3.4.4 and
binutils 2.16.1.

int main(int argc, char **argv)
{
        static long int random(void);
        random();
        return 0;
}

Declaring random() as a static function is definitely wrong, but I
wonder why none of gcc, as, or ld give me any error or warning.

With -O2, gcc outputs:

        .set noreorder
        ...
        lw $25,%got(random)($28)
        nop
        addiu $25,$25,%lo(random)
        jalr $25
        nop

This does not work.

With -O, gcc outputs:

        .set reorder
        ...
        la $25,random
        jal $25

This works.

If I correctly declared random() as extern function, gcc -O2 outputs:

        .set noreorder
        ...
        lw $25,%call16(random)($28)
        nop
        jalr $25

This works (of course).

In first case, final output is:

  4008c8: 8f998058 lw t9,-32680(gp)
  4008cc: 00000000 nop
  4008d0: 27390000 addiu t9,t9,0
  4008d4: 0320f809 jalr t9
  4008d8: 00000000 nop

where value in GOT (-32680(gp)) is 00000000.


Is it possible to detect this sort of error when compiling or linking?

---
Atsushi Nemoto
I'm not on this ML.  Please CC to me.  Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: Detecting wrong linkage on mips PIC code

Maciej W. Rozycki
On Fri, 24 Jun 2005, Atsushi Nemoto wrote:

> This simple queer program does not work on mips-linux if compiled with
> -O2 option.  It works if compiled with -O.  I'm using gcc 3.4.4 and
> binutils 2.16.1.
>
> int main(int argc, char **argv)
> {
> static long int random(void);
> random();
> return 0;
> }
>
> Declaring random() as a static function is definitely wrong, but I
> wonder why none of gcc, as, or ld give me any error or warning.
[...]
> Is it possible to detect this sort of error when compiling or linking?

 It is.  With GCC 4.0.0 I get:

static.c: In function 'main':
static.c:3: error: invalid storage class for function 'random'

Also this program, which is similarly incorrect:

long int random(void);

int main(void)
{
        random();
        return 0;
}

static long int random(void)
{
        return 0;
}

compiles to bad code with GCC 3.4.x and is rejected by 4.0.0.

  Maciej
Reply | Threaded
Open this post in threaded view
|

Re: Detecting wrong linkage on mips PIC code

Atsushi Nemoto
>>>>> On Thu, 23 Jun 2005 18:01:37 +0100 (BST), "Maciej W. Rozycki" <[hidden email]> said:

>> Is it possible to detect this sort of error when compiling or linking?
macro>  It is.  With GCC 4.0.0 I get:

macro> static.c: In function 'main':
macro> static.c:3: error: invalid storage class for function 'random'

Thank you.  That's what I want.  And grepping the error message in gcc
3.4.x tree, I found that gcc 3.4 also can warn it by "-pedantic"
option.

---
Atsushi Nemoto