[v2,gdb/testsuite] Fix gdb.base/enum_cond.exp on arm-linux

Message ID 20250318072806.23981-1-tdevries@suse.de
State Committed
Headers
Series [v2,gdb/testsuite] Fix gdb.base/enum_cond.exp on arm-linux |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 fail Patch failed to apply

Commit Message

Tom de Vries March 18, 2025, 7:28 a.m. UTC
  On arm-linux, I run into:
...
gdb compile failed, ld: warning: enum_cond.o uses variable-size enums yet \
  the output is to use 32-bit enums; use of enum values across objects may fail
UNTESTED: gdb.base/enum_cond.exp: failed to compile
...

Fix this by using -nostdlib.

Tested on arm-linux and x86_64-linux.
---
 gdb/testsuite/gdb.base/enum_cond.c   | 16 ++++++++++++++++
 gdb/testsuite/gdb.base/enum_cond.exp | 15 ++++++++++++++-
 gdb/testsuite/lib/gdb.exp            | 12 ++++++++++++
 3 files changed, 42 insertions(+), 1 deletion(-)


base-commit: 7b2a5f7183b946a0a2644a505892b7e0aefbbfd1
  

Comments

Simon Marchi March 18, 2025, 1:46 p.m. UTC | #1
On 2025-03-18 03:28, Tom de Vries wrote:
> On arm-linux, I run into:
> ...
> gdb compile failed, ld: warning: enum_cond.o uses variable-size enums yet \
>   the output is to use 32-bit enums; use of enum values across objects may fail
> UNTESTED: gdb.base/enum_cond.exp: failed to compile
> ...
> 
> Fix this by using -nostdlib.
> 
> Tested on arm-linux and x86_64-linux.
> ---
>  gdb/testsuite/gdb.base/enum_cond.c   | 16 ++++++++++++++++
>  gdb/testsuite/gdb.base/enum_cond.exp | 15 ++++++++++++++-
>  gdb/testsuite/lib/gdb.exp            | 12 ++++++++++++
>  3 files changed, 42 insertions(+), 1 deletion(-)
> 
> diff --git a/gdb/testsuite/gdb.base/enum_cond.c b/gdb/testsuite/gdb.base/enum_cond.c
> index 35e126aed45..df8830f0c9e 100644
> --- a/gdb/testsuite/gdb.base/enum_cond.c
> +++ b/gdb/testsuite/gdb.base/enum_cond.c
> @@ -46,3 +46,19 @@ main (void)
>    return 0;
>  }
>  
> +void
> +exit (int status)
> +{
> +#if HAVE_BUILTIN_TRAP
> +  __builtin_trap ();
> +#endif
> +  while (1)
> +    ;

Huh, it would be nice to find something that works everywhere.  We do
have "my_exit" in lib/my-syscalls.h, which we could call, but we would
need to provide a syscall implementation for all arches we want to test
(not too hard, but still).

What about having a global:

  volatile int *dummy = nullptr;

and then do:

  *dummy = 1;

Would that successfully stop the program on all known platforms?

Simon
  
Tom de Vries March 18, 2025, 2:08 p.m. UTC | #2
On 3/18/25 14:46, Simon Marchi wrote:
> 
> 
> On 2025-03-18 03:28, Tom de Vries wrote:
>> On arm-linux, I run into:
>> ...
>> gdb compile failed, ld: warning: enum_cond.o uses variable-size enums yet \
>>    the output is to use 32-bit enums; use of enum values across objects may fail
>> UNTESTED: gdb.base/enum_cond.exp: failed to compile
>> ...
>>
>> Fix this by using -nostdlib.
>>
>> Tested on arm-linux and x86_64-linux.
>> ---
>>   gdb/testsuite/gdb.base/enum_cond.c   | 16 ++++++++++++++++
>>   gdb/testsuite/gdb.base/enum_cond.exp | 15 ++++++++++++++-
>>   gdb/testsuite/lib/gdb.exp            | 12 ++++++++++++
>>   3 files changed, 42 insertions(+), 1 deletion(-)
>>
>> diff --git a/gdb/testsuite/gdb.base/enum_cond.c b/gdb/testsuite/gdb.base/enum_cond.c
>> index 35e126aed45..df8830f0c9e 100644
>> --- a/gdb/testsuite/gdb.base/enum_cond.c
>> +++ b/gdb/testsuite/gdb.base/enum_cond.c
>> @@ -46,3 +46,19 @@ main (void)
>>     return 0;
>>   }
>>   
>> +void
>> +exit (int status)
>> +{
>> +#if HAVE_BUILTIN_TRAP
>> +  __builtin_trap ();
>> +#endif
>> +  while (1)
>> +    ;
> 
> Huh, it would be nice to find something that works everywhere.
> We do
> have "my_exit" in lib/my-syscalls.h, which we could call, but we would
> need to provide a syscall implementation for all arches we want to test
> (not too hard, but still).
> 
> What about having a global:
> 
>    volatile int *dummy = nullptr;
> 
> and then do:
> 
>    *dummy = 1;
> 
> Would that successfully stop the program on all known platforms?

AFAIU that's undefined behaviour, so a valid implementation might just 
skip the store.

I can submit a v3 using this (note NULL instead of nullptr since this is 
C, not C++):
...
void
exit (int status)
{
#if HAVE_BUILTIN_TRAP
   __builtin_trap ();
#endif

   {
     volatile int *dummy = NULL;
     *dummy = 1;
   }

   while (1)
     ;
...
but AFAIC I'm fine with this v2.

Thanks,
- Tom
  
Simon Marchi March 18, 2025, 2:47 p.m. UTC | #3
On 3/18/25 10:08 AM, Tom de Vries wrote:
> On 3/18/25 14:46, Simon Marchi wrote:
>>
>>
>> On 2025-03-18 03:28, Tom de Vries wrote:
>>> On arm-linux, I run into:
>>> ...
>>> gdb compile failed, ld: warning: enum_cond.o uses variable-size enums yet \
>>>    the output is to use 32-bit enums; use of enum values across objects may fail
>>> UNTESTED: gdb.base/enum_cond.exp: failed to compile
>>> ...
>>>
>>> Fix this by using -nostdlib.
>>>
>>> Tested on arm-linux and x86_64-linux.
>>> ---
>>>   gdb/testsuite/gdb.base/enum_cond.c   | 16 ++++++++++++++++
>>>   gdb/testsuite/gdb.base/enum_cond.exp | 15 ++++++++++++++-
>>>   gdb/testsuite/lib/gdb.exp            | 12 ++++++++++++
>>>   3 files changed, 42 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/gdb/testsuite/gdb.base/enum_cond.c b/gdb/testsuite/gdb.base/enum_cond.c
>>> index 35e126aed45..df8830f0c9e 100644
>>> --- a/gdb/testsuite/gdb.base/enum_cond.c
>>> +++ b/gdb/testsuite/gdb.base/enum_cond.c
>>> @@ -46,3 +46,19 @@ main (void)
>>>     return 0;
>>>   }
>>>   +void
>>> +exit (int status)
>>> +{
>>> +#if HAVE_BUILTIN_TRAP
>>> +  __builtin_trap ();
>>> +#endif
>>> +  while (1)
>>> +    ;
>>
>> Huh, it would be nice to find something that works everywhere.
>> We do
>> have "my_exit" in lib/my-syscalls.h, which we could call, but we would
>> need to provide a syscall implementation for all arches we want to test
>> (not too hard, but still).
>>
>> What about having a global:
>>
>>    volatile int *dummy = nullptr;
>>
>> and then do:
>>
>>    *dummy = 1;
>>
>> Would that successfully stop the program on all known platforms?
> 
> AFAIU that's undefined behaviour, so a valid implementation might just skip the store.
> 
> I can submit a v3 using this (note NULL instead of nullptr since this is C, not C++):
> ...
> void
> exit (int status)
> {
> #if HAVE_BUILTIN_TRAP
>   __builtin_trap ();
> #endif
> 
>   {
>     volatile int *dummy = NULL;
>     *dummy = 1;
>   }
> 
>   while (1)
>     ;
> ...
> but AFAIC I'm fine with this v2.
> 
> Thanks,
> - Tom

Ok, fair enough, that's a very edgy case anyway.

Approved-By: Simon Marchi <simon.marchi@efficios.com>

Simon
  

Patch

diff --git a/gdb/testsuite/gdb.base/enum_cond.c b/gdb/testsuite/gdb.base/enum_cond.c
index 35e126aed45..df8830f0c9e 100644
--- a/gdb/testsuite/gdb.base/enum_cond.c
+++ b/gdb/testsuite/gdb.base/enum_cond.c
@@ -46,3 +46,19 @@  main (void)
   return 0;
 }
 
+void
+exit (int status)
+{
+#if HAVE_BUILTIN_TRAP
+  __builtin_trap ();
+#endif
+  while (1)
+    ;
+}
+
+void
+_start (void)
+{
+  main ();
+  exit (0);
+}
diff --git a/gdb/testsuite/gdb.base/enum_cond.exp b/gdb/testsuite/gdb.base/enum_cond.exp
index 3ee90f8721f..d58da4796fe 100644
--- a/gdb/testsuite/gdb.base/enum_cond.exp
+++ b/gdb/testsuite/gdb.base/enum_cond.exp
@@ -19,7 +19,20 @@ 
 
 standard_testfile .c
 
-set opts [list debug additional_flags=-fshort-enums]
+set opts {}
+lappend opts debug
+lappend opts additional_flags=-fshort-enums
+# Without -nostdlib, on arm we run into:
+#
+#   ld: warning: enum_cond.o uses variable-size enums yet the output is to use
+#     32-bit enums; use of enum values across objects may fail
+#
+# due to conflicting values for Tag_ABI_enum_size between enum_cond.o and
+# linked-in objects.  Work around this by using -nostdlib, making sure there's
+# just one object, and no such conflict can happen.
+lappend opts additional_flags=-nostdlib
+lappend opts additional_flags=-DHAVE_BUILTIN_TRAP=[have_builtin_trap]
+
 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable $opts] != "" } {
     untested "failed to compile"
     return -1
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 481f9ecdf3d..3349da7a263 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -11072,5 +11072,17 @@  proc section_get {exec section} {
     return $retval
 }
 
+# Return 1 if the compiler supports __builtin_trap, else return 0.
+
+gdb_caching_proc have_builtin_trap {} {
+
+    return [gdb_can_simple_compile builtin_trap {
+	int main() {
+	    __builtin_trap ();
+	    return 0;
+	}
+    } executable]
+}
+
 # Always load compatibility stuff.
 load_lib future.exp