Subject: Warning: gcc -O2 -fpic defect

From: Alexander Elkins <[email-masked]>

I traced a failure in some code I was trying to run to defective code
produced by gcc (from the 1.0-pre-release) for a shared library I also
built.  The attached test program demonstrates the defect.  I was wondering
if any of the newer versions of gcc have this defect fixed?  Otherwise, as a
workaround don't use -O[12] -fpic with gcc.  Compile with the -S option to
look at gcc-tst.s and you'll see register a5 is associated with two
parameters, i.e. the value of one overwrites the other.

 - Alexander Elkins

----------------------------->8 cut here 8<----------------------------------
/* gcc -o gcc-tst -O2 -fpic gcc-tst.c;./gcc-tst # Fails! */
/* gcc -o gcc-tst -O1 -fpic gcc-tst.c;./gcc-tst # Fails! */
/* gcc -o gcc-tst -O2       gcc-tst.c;./gcc-tst # OK.    */
/* gcc -o gcc-tst     -fpic gcc-tst.c;./gcc-tst # OK.    */
#include <stdlib.h>
#include <stdio.h>

void prtv(int number, int value)
{
    printf("v%d = %d\n", number, value);
}

int ftst(int v1, int v2,  int v3,  int v4,  int v5,  int v6,  int v7,  int v8,
         int v9, int v10, int v11, int v12, int v13, int v14, int v15, int v16)
{
    printf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
        v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16);
    return v1  ==  1 && v2  ==  2 && v3  ==  3 && v4  ==  4 &&
           v5  ==  5 && v6  ==  6 && v7  ==  7 && v8  ==  8 &&
           v9  ==  9 && v10 == 10 && v11 == 11 && v12 == 12 &&
           v13 == 13 && v14 == 14 && v15 == 15 && v16 == 16;
} 

int rtst(int v1, int v2,  int v3,  int v4,  int v5,  int v6,  int v7,  int v8,
         int v9, int v10, int v11, int v12, int v13, int v14, int v15, int v16)
{
    prtv(1, v1);
    return ftst(v16,v15,v14,v13,v12,v11,v10,v9,v8,v7,v6,v5,v4,v3,v2,v1);
}

int main(void)
{
    int result = ftst(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) &&
                 rtst(16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1);
    puts(result ? "OK." : "Failed!");
    return result ? EXIT_SUCCESS : EXIT_FAILURE;
}