Broken RISC-V code in newlib

Eric Salem ericsalem@gmail.com
Tue Mar 25 15:48:53 GMT 2025


On 3/25/25 10:07 AM, Jeff Law wrote:
> On 3/25/25 8:12 AM, Eric Salem wrote:
> 
>>
>> 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.
> Yea, I can throw it into the tester, it's got provisions for this kind of evaluation.  Results in an hour or so depending on my morning meeting schedule ;-)
> 
> jeff

Updated patch for you. I didn't add underscores as a prefix to the new type since Newlib isn't doing that for its existing typedefs, so I figured to make it consistent.

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/strlen.c b/newlib/libc/machine/riscv/strlen.c
index 7e5d41617eac..398c4b426676 100644
--- a/newlib/libc/machine/riscv/strlen.c
+++ b/newlib/libc/machine/riscv/strlen.c
@@ -9,9 +9,9 @@
    http://www.opensource.org/licenses.
 */
 
-#include <string.h>
+#include <stddef.h>
 #include <stdint.h>
-#include "sys/asm.h"
+#include "sys/string.h"
 
 size_t strlen(const char *str)
 {
diff --git a/newlib/libc/machine/riscv/sys/asm.h b/newlib/libc/machine/riscv/sys/asm.h
index 0a354b220517..8c8aeb3ae775 100644
--- a/newlib/libc/machine/riscv/sys/asm.h
+++ b/newlib/libc/machine/riscv/sys/asm.h
@@ -12,8 +12,6 @@
 #ifndef _SYS_ASM_H
 #define _SYS_ASM_H
 
-#include <stdint.h>
-
 /*
  * Macros to handle different pointer/register sizes for 32/64-bit code
  */
@@ -22,13 +20,11 @@
 # define SZREG	8
 # define REG_S sd
 # define REG_L ld
-typedef uint64_t uintxlen_t;
 #elif __riscv_xlen == 32
 # define PTRLOG 2
 # define SZREG	4
 # define REG_S sw
 # define REG_L lw
-typedef uint32_t uintxlen_t;
 #else
 # error __riscv_xlen must equal 32 or 64
 #endif
diff --git a/newlib/libc/machine/riscv/sys/string.h b/newlib/libc/machine/riscv/sys/string.h
index b65635cb6cb6..f72ffa6caac1 100644
--- a/newlib/libc/machine/riscv/sys/string.h
+++ b/newlib/libc/machine/riscv/sys/string.h
@@ -13,7 +13,7 @@
 #define _SYS_STRING_H
 
 #include <stdbool.h>
-#include "asm.h"
+#include "xlenint.h"
 
 #if __riscv_zbb
   #include <riscv_bitmanip.h>
diff --git a/newlib/libc/machine/riscv/sys/xlenint.h b/newlib/libc/machine/riscv/sys/xlenint.h
new file mode 100644
index 000000000000..37f0ac81f307
--- /dev/null
+++ b/newlib/libc/machine/riscv/sys/xlenint.h
@@ -0,0 +1,14 @@
+#ifndef _SYS_XLENINT_H
+#define _SYS_XLENINT_H
+
+#include <stdint.h>
+
+#if __riscv_xlen == 64
+typedef uint64_t uintxlen_t;
+#elif __riscv_xlen == 32
+typedef uint32_t uintxlen_t;
+#else
+# error __riscv_xlen must equal 32 or 64
+#endif
+
+#endif /* sys/xlenint.h */


More information about the Newlib mailing list