diff mbox

[testsuite,Android] Add <stdlib.h> to pr56407.c

Message ID CACysShjXTPPE1E36a60J9A9TosTS=GzC+zYTkOaOHogKZoymQA@mail.gmail.com
State New
Headers show

Commit Message

Alexander Ivchenko Aug. 5, 2013, 10:57 a.m. UTC
Hi,

The following test case fails to compile on Android: gcc.dg/torture/pr56407.c

/tmp/ccA08Isw.o:pr56407.c:function test: error: undefined reference to 'rand'
collect2: error: ld returned 1 exit status

Which is not surprising at all, since the testcase has only the
declarations of abort() and rand()
and doesn't have any headers included.

The following patch adds <stdlib.h> to the test.
Tested on x86_64-unknown-linux-gnu and on Android. Also I checked that
the original regression appears on the fixed test.

Is it OK for trunk?



--Alexander

Comments

Maxim Kuvyrkov Aug. 5, 2013, 7:07 p.m. UTC | #1
On 5/08/2013, at 10:57 PM, Alexander Ivchenko wrote:

> Hi,
> 
> The following test case fails to compile on Android: gcc.dg/torture/pr56407.c
> 
> /tmp/ccA08Isw.o:pr56407.c:function test: error: undefined reference to 'rand'
> collect2: error: ld returned 1 exit status
> 
> Which is not surprising at all, since the testcase has only the
> declarations of abort() and rand()
> and doesn't have any headers included.

It *is* surprising given that the testcase does have declarations of abort() or rand() -- which are the only two external functions referenced.

> 
> The following patch adds <stdlib.h> to the test.

I don't think this a correct fix.  [In most such cases the real problem can be found out by examining why linking a test against glibc works, but against bionic -- doesn't.]

How are you linking this testcase?  Please post both the link line from testsuite log and verbose output of linking (obtainable by adding "-v" to the link line).

Thank you,

--
Maxim Kuvyrkov
www.kugelworks.com
Alexander Ivchenko Aug. 6, 2013, 11:58 a.m. UTC | #2
The reason for undefined reference to rand is that it is defined as
"static __inline__" in Bionic stdlib.h:

static __inline__ int rand(void) {
    return (int)lrand48();
}

So in fact, if you do "nm libc.so" for Bionic, you won't get the
rand.. which is probably not correct, because it doesn't have the
external linkage. I added Elliott.

Still, in C standart it is said that rand is defined in stdlib.h, and
we don't include it in that testcase.


thanks,
Alexander


2013/8/5 Maxim Kuvyrkov <maxim@kugelworks.com>:
> On 5/08/2013, at 10:57 PM, Alexander Ivchenko wrote:
>
>> Hi,
>>
>> The following test case fails to compile on Android: gcc.dg/torture/pr56407.c
>>
>> /tmp/ccA08Isw.o:pr56407.c:function test: error: undefined reference to 'rand'
>> collect2: error: ld returned 1 exit status
>>
>> Which is not surprising at all, since the testcase has only the
>> declarations of abort() and rand()
>> and doesn't have any headers included.
>
> It *is* surprising given that the testcase does have declarations of abort() or rand() -- which are the only two external functions referenced.
>
>>
>> The following patch adds <stdlib.h> to the test.
>
> I don't think this a correct fix.  [In most such cases the real problem can be found out by examining why linking a test against glibc works, but against bionic -- doesn't.]
>
> How are you linking this testcase?  Please post both the link line from testsuite log and verbose output of linking (obtainable by adding "-v" to the link line).
>
> Thank you,
>
> --
> Maxim Kuvyrkov
> www.kugelworks.com
>
>
Andreas Schwab Aug. 6, 2013, 12:04 p.m. UTC | #3
Alexander Ivchenko <aivchenk@gmail.com> writes:

> Still, in C standart it is said that rand is defined in stdlib.h, and
> we don't include it in that testcase.

It also says: "Provided that a library function can be declared without
reference to any type defined in a header, it is also permissible to
declare the function and use it without including its associated
header."

Andreas.
Alexander Ivchenko Aug. 6, 2013, 12:42 p.m. UTC | #4
Thanks Andreas, it seems clear now that Bionic rand function is not
consistent with the standart.


--Alexander

2013/8/6 Andreas Schwab <schwab@suse.de>:
> Alexander Ivchenko <aivchenk@gmail.com> writes:
>
>> Still, in C standart it is said that rand is defined in stdlib.h, and
>> we don't include it in that testcase.
>
> It also says: "Provided that a library function can be declared without
> reference to any type defined in a header, it is also permissible to
> declare the function and use it without including its associated
> header."
>
> Andreas.
>
> --
> Andreas Schwab, SUSE Labs, schwab@suse.de
> GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
> "And now for something completely different."
diff mbox

Patch

diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index f356d55..5c23650 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@ 
+2013-08-05  Alexander Ivchenko  <alexander.ivchenko@intel.com>
+
+       * gcc.dg/torture/pr56407.c: Add include of stdlib.h. Remove
+       declaration of abort() and rand().
+
 2013-08-04  Ed Smith-Rowland  <3dw4rd@verizon.net>

        PR c++/58072
diff --git a/gcc/testsuite/gcc.dg/torture/pr56407.c
b/gcc/testsuite/gcc.dg/torture/pr56407.c
index f26fd23..5d35874 100644
--- a/gcc/testsuite/gcc.dg/torture/pr56407.c
+++ b/gcc/testsuite/gcc.dg/torture/pr56407.c
@@ -1,7 +1,6 @@ 
 /* { dg-do run } */

-extern void abort(void);
-extern int rand(void);
+#include <stdlib.h>

 static void copy(int *r,int *a,int na)
 {