Re: Broken RISC-V code in newlib

Eric Salem <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>

On 3/25/25 8:40 AM, Jeff Law wrote:
> 
> You recently put a typedef into libc/machine/riscv/sys/asm.h as well as including stdint.h.
> 
> That breaks code such as strcmp.S which includes sys/asm.h, but is processed by the assembler after pre-processing:
> 
>>   CPPAS    libc/machine/riscv/libc_a-strcmp.o
>> /home/jlaw/jenkins/workspace/riscv64-elf/newlib-cygwin/newlib/libc/include/machine/_default_types.h: Assembler messages:
>> /home/jlaw/jenkins/workspace/riscv64-elf/newlib-cygwin/newlib/libc/include/machine/_default_types.h:41: Error: unrecognized opcode `typedef signed char __int8_t'
>> /home/jlaw/jenkins/workspace/riscv64-elf/newlib-cygwin/newlib/libc/include/machine/_default_types.h:43: Error: unrecognized opcode `typedef unsigned char __uint8_t'
>> /home/jlaw/jenkins/workspace/riscv64-elf/newlib-cygwin/newlib/libc/include/machine/_default_types.h:55: Error: unrecognized opcode `typedef short int __int16_t'
>> /home/jlaw/jenkins/workspace/riscv64-elf/newlib-cygwin/newlib/libc/include/machine/_default_types.h:57: Error: unrecognized opcode `typedef short unsigned int __uint16_t'
>> /home/jlaw/jenkins/workspace/riscv64-elf/newlib-cygwin/newlib/libc/include/machine/_default_types.h:77: Error: unrecognized opcode `typedef int __int32_t'
>> /home/jlaw/jenkins/workspace/riscv64-elf/newlib-cygwin/newlib/libc/include/machine/_default_types.h:79: Error: unrecognized opcode `typedef unsigned int __uint32_t'
> [ ... ]
> 
>>   CPPAS    libc/machine/riscv/libc_a-setjmp.o
>> /home/jlaw/jenkins/workspace/riscv64-elf/newlib-cygwin/newlib/libc/include/machine/_default_types.h: Assembler messages:
>> /home/jlaw/jenkins/workspace/riscv64-elf/newlib-cygwin/newlib/libc/include/machine/_default_types.h:41: Error: unrecognized opcode `typedef signed char __int8_t'
>> /home/jlaw/jenkins/workspace/riscv64-elf/newlib-cygwin/newlib/libc/include/machine/_default_types.h:43: Error: unrecognized opcode `typedef unsigned char __uint8_t'
>> /home/jlaw/jenkins/workspace/riscv64-elf/newlib-cygwin/newlib/libc/include/machine/_default_types.h:55: Error: unrecognized opcode `typedef short int __int16_t'
>> /home/jlaw/jenkins/workspace/riscv64-elf/newlib-cygwin/newlib/libc/include/machine/_default_types.h:57: Error: unrecognized opcode `typedef short unsigned int __uint16_t'
>> /home/jlaw/jenkins/workspace/riscv64-elf/newlib-cygwin/newlib/libc/include/machine/_default_types.h:77: Error: unrecognized opcode `typedef int __int32_t'
> 
> 
> 
> Those two failures make it appear that this change has not been tested at all.
> 
> And it looks like you busted strcpy as well.  This probably only fails with gcc-15 as gcc-15 defaults to c23 where implicit declarations are flagged as hard errors:
> 
>>  CC       libc/machine/riscv/libc_a-strcpy.o
>> ../../../..//newlib-cygwin/newlib/libc/machine/riscv/strcpy.c: In function 'strcpy':
>> ../../../..//newlib-cygwin/newlib/libc/machine/riscv/strcpy.c:17:10: error: implicit declaration of function '__libc_strcpy'; did you mean '__builtin_strcpy'? [-Wimplicit-function-declaration]
>>    17 |   return __libc_strcpy(dst, src, true);
>>       |          ^~~~~~~~~~~~~
>>       |          __builtin_strcpy
>> ../../../..//newlib-cygwin/newlib/libc/machine/riscv/strcpy.c:17:10: error: returning 'int' from a function with return type 'char *' makes pointer from integer without a cast [-Wint-conversion]
>>    17 |   return __libc_strcpy(dst, src, true);
>>       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> And stpcpy:
>>   CC       libc/machine/riscv/libc_a-stpcpy.o
>> ../../../..//newlib-cygwin/newlib/libc/machine/riscv/stpcpy.c: In function 'stpcpy':
>> ../../../..//newlib-cygwin/newlib/libc/machine/riscv/stpcpy.c:6:10: error: implicit declaration of function '__libc_strcpy'; did you mean '__builtin_strcpy'? [-Wimplicit-function-declaration]
>>     6 |   return __libc_strcpy(dst, src, false);
>>       |          ^~~~~~~~~~~~~
>>       |          __builtin_strcpy
>> ../../../..//newlib-cygwin/newlib/libc/machine/riscv/stpcpy.c:6:10: error: returning 'int' from a function with return type 'char *' makes pointer from integer without a cast [-Wint-conversion]
>>     6 |   return __libc_strcpy(dst, src, false);
>>       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> make[1]: *** [Makefile:34809: libc/machine/riscv/libc_a-stpcpy.o] Error 1
> 
> 
> 
> Jeff

I'm having a hard time recreating this on my machine, which is why I wouldn't have gotten these errors. Can you apply the below patch? It should address the errors. I want to make sure it works for you before emailing the list with a fix.

diff --git a/newlib/libc/machine/riscv/stpcpy.c b/newlib/libc/machine/riscv/stpcpy.c
index 9243457b25a2..0c545623ba9e 100644
--- a/newlib/libc/machine/riscv/stpcpy.c
+++ b/newlib/libc/machine/riscv/stpcpy.c
@@ -1,5 +1,5 @@
-#include <string.h>
 #include <stdbool.h>
+#include "sys/string.h"
 
 char *stpcpy(char *dst, const char *src)
 {
diff --git a/newlib/libc/machine/riscv/strcpy.c b/newlib/libc/machine/riscv/strcpy.c
index f770493fbc2d..856b66ebc801 100644
--- a/newlib/libc/machine/riscv/strcpy.c
+++ b/newlib/libc/machine/riscv/strcpy.c
@@ -9,8 +9,8 @@
    http://www.opensource.org/licenses.
 */
 
-#include <string.h>
 #include <stdbool.h>
+#include "sys/string.h"
 
 char *strcpy(char *dst, const char *src)
 {
diff --git a/newlib/libc/machine/riscv/sys/asm.h b/newlib/libc/machine/riscv/sys/asm.h
index 0a354b220517..a9792e964ebc 100644
--- a/newlib/libc/machine/riscv/sys/asm.h
+++ b/newlib/libc/machine/riscv/sys/asm.h
@@ -12,7 +12,9 @@
 #ifndef _SYS_ASM_H
 #define _SYS_ASM_H
 
+#if !__ASSEMBLER__
 #include <stdint.h>
+#endif
 
 /*
  * Macros to handle different pointer/register sizes for 32/64-bit code
@@ -22,13 +24,17 @@
 # define SZREG	8
 # define REG_S sd
 # define REG_L ld
+#if !__ASSEMBLER__
 typedef uint64_t uintxlen_t;
+#endif
 #elif __riscv_xlen == 32
 # define PTRLOG 2
 # define SZREG	4
 # define REG_S sw
 # define REG_L lw
+#if !__ASSEMBLER__
 typedef uint32_t uintxlen_t;
+#endif
 #else
 # error __riscv_xlen must equal 32 or 64
 #endif
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.