From abeef87f640a0d7e6281972c1ee0dd992744f0ad Mon Sep 17 00:00:00 2001 From: Sang Woo Kim Date: Wed, 22 Jul 2026 05:02:00 +0000 Subject: [PATCH] newlib: fix RTEMS socket address definitions and constants RTEMS was using the generic ARM socket address layout, which does not match its FreeBSD-derived socket API. As a result, std could not parse addresses returned by local_addr, accept, and recv_from. Use the correct RTEMS socket address definitions and update 27 constants to their FreeBSD values. Header sources (newlib as built by the RTEMS 6 RSB, gitlab.rtems.org/contrib/newlib-cygwin @ 1b3dcfdc6f1f): https://gitlab.rtems.org/contrib/newlib-cygwin/-/blob/1b3dcfdc6f1fd2bd3e1ef2f8b7df736c076c6042/newlib/libc/sys/rtems/include/sys/socket.h#L322 https://gitlab.rtems.org/contrib/newlib-cygwin/-/blob/1b3dcfdc6f1fd2bd3e1ef2f8b7df736c076c6042/newlib/libc/sys/rtems/include/netinet/in.h#L98 https://gitlab.rtems.org/contrib/newlib-cygwin/-/blob/1b3dcfdc6f1fd2bd3e1ef2f8b7df736c076c6042/newlib/libc/sys/rtems/include/netinet6/in6.h#L121 https://gitlab.rtems.org/contrib/newlib-cygwin/-/blob/1b3dcfdc6f1fd2bd3e1ef2f8b7df736c076c6042/newlib/libc/sys/rtems/include/sys/un.h#L58 https://gitlab.rtems.org/contrib/newlib-cygwin/-/blob/1b3dcfdc6f1fd2bd3e1ef2f8b7df736c076c6042/newlib/libc/sys/rtems/include/sys/_sockaddr_storage.h#L41-53 https://gitlab.rtems.org/contrib/newlib-cygwin/-/blob/1b3dcfdc6f1fd2bd3e1ef2f8b7df736c076c6042/newlib/libc/sys/rtems/include/sys/socket.h#L246 https://gitlab.rtems.org/contrib/newlib-cygwin/-/blob/1b3dcfdc6f1fd2bd3e1ef2f8b7df736c076c6042/newlib/libc/sys/rtems/include/netinet/in.h#L424 https://gitlab.rtems.org/contrib/newlib-cygwin/-/blob/1b3dcfdc6f1fd2bd3e1ef2f8b7df736c076c6042/newlib/libc/sys/rtems/include/netinet/tcp.h#L168 https://gitlab.rtems.org/contrib/newlib-cygwin/-/blob/1b3dcfdc6f1fd2bd3e1ef2f8b7df736c076c6042/newlib/libc/sys/rtems/include/sys/poll.h#L65 https://gitlab.rtems.org/contrib/newlib-cygwin/-/blob/1b3dcfdc6f1fd2bd3e1ef2f8b7df736c076c6042/newlib/libc/sys/rtems/include/sys/filio.h#L50 https://gitlab.rtems.org/contrib/newlib-cygwin/-/blob/1b3dcfdc6f1fd2bd3e1ef2f8b7df736c076c6042/newlib/libc/sys/rtems/include/sys/_termios.h#L78 https://gitlab.rtems.org/contrib/newlib-cygwin/-/blob/1b3dcfdc6f1fd2bd3e1ef2f8b7df736c076c6042/newlib/libc/sys/rtems/include/netdb.h#L156 https://gitlab.rtems.org/contrib/newlib-cygwin/-/blob/1b3dcfdc6f1fd2bd3e1ef2f8b7df736c076c6042/newlib/libc/include/sys/_default_fcntl.h#L63 https://gitlab.rtems.org/contrib/newlib-cygwin/-/blob/1b3dcfdc6f1fd2bd3e1ef2f8b7df736c076c6042/newlib/libc/include/sys/select.h#L34 --- src/unix/newlib/arm/mod.rs | 52 ------------------------------- src/unix/newlib/mod.rs | 58 ++++++++++++++++++++++++----------- src/unix/newlib/rtems/mod.rs | 59 ++++++++++++++++++++++++++++++++++-- 3 files changed, 96 insertions(+), 73 deletions(-) delete mode 100644 src/unix/newlib/arm/mod.rs diff --git a/src/unix/newlib/arm/mod.rs b/src/unix/newlib/arm/mod.rs deleted file mode 100644 index a559cf4da59fd..0000000000000 --- a/src/unix/newlib/arm/mod.rs +++ /dev/null @@ -1,52 +0,0 @@ -use crate::prelude::*; - -pub type clock_t = c_long; -pub type wchar_t = u32; - -s! { - pub struct sockaddr { - pub sa_family: crate::sa_family_t, - pub sa_data: [c_char; 14], - } - - pub struct sockaddr_in6 { - pub sin6_family: crate::sa_family_t, - pub sin6_port: crate::in_port_t, - pub sin6_flowinfo: u32, - pub sin6_addr: crate::in6_addr, - pub sin6_scope_id: u32, - } - - pub struct sockaddr_in { - pub sin_family: crate::sa_family_t, - pub sin_port: crate::in_port_t, - pub sin_addr: crate::in_addr, - pub sin_zero: [u8; 8], - } - - pub struct sockaddr_storage { - pub ss_family: crate::sa_family_t, - __ss_padding: Padding<[u8; 26]>, - } -} - -pub const AF_INET6: c_int = 23; - -pub const FIONBIO: c_ulong = 1; - -pub const POLLIN: c_short = 0x1; -pub const POLLPRI: c_short = 0x2; -pub const POLLHUP: c_short = 0x4; -pub const POLLERR: c_short = 0x8; -pub const POLLOUT: c_short = 0x10; -pub const POLLNVAL: c_short = 0x20; - -pub const SOL_SOCKET: c_int = 65535; - -pub const MSG_OOB: c_int = 1; -pub const MSG_PEEK: c_int = 2; -pub const MSG_DONTWAIT: c_int = 4; -pub const MSG_DONTROUTE: c_int = 0; -pub const MSG_WAITALL: c_int = 0; -pub const MSG_MORE: c_int = 0; -pub const MSG_NOSIGNAL: c_int = 0; diff --git a/src/unix/newlib/mod.rs b/src/unix/newlib/mod.rs index f5c7396d67d94..335f959f3646a 100644 --- a/src/unix/newlib/mod.rs +++ b/src/unix/newlib/mod.rs @@ -368,6 +368,8 @@ pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = pthread_rwlock_t { cfg_if! { if #[cfg(target_os = "espidf")] { pub const NCCS: usize = 11; + } else if #[cfg(target_os = "rtems")] { + pub const NCCS: usize = 20; } else { pub const NCCS: usize = 32; } @@ -427,7 +429,7 @@ pub const PTHREAD_MUTEX_ERRORCHECK: c_int = 2; cfg_if! { if #[cfg(any(target_os = "horizon", target_os = "espidf"))] { pub const FD_SETSIZE: c_int = 64; - } else if #[cfg(target_os = "vita")] { + } else if #[cfg(any(target_os = "vita", target_os = "rtems"))] { pub const FD_SETSIZE: c_int = 256; } else { pub const FD_SETSIZE: c_int = 1024; @@ -559,7 +561,7 @@ pub const O_NONBLOCK: c_int = 16384; pub const O_ACCMODE: c_int = 3; cfg_if! { - if #[cfg(target_os = "espidf")] { + if #[cfg(any(target_os = "espidf", target_os = "rtems"))] { pub const O_CLOEXEC: c_int = 0x40000; } else { pub const O_CLOEXEC: c_int = 0x80000; @@ -605,6 +607,8 @@ pub const PF_INET: c_int = 2; cfg_if! { if #[cfg(target_os = "espidf")] { pub const PF_INET6: c_int = 10; + } else if #[cfg(target_os = "rtems")] { + pub const PF_INET6: c_int = 28; } else { pub const PF_INET6: c_int = 23; } @@ -666,7 +670,13 @@ cfg_if! { } pub const SO_TYPE: c_int = 0x1008; -pub const SOCK_CLOEXEC: c_int = O_CLOEXEC; +cfg_if! { + if #[cfg(target_os = "rtems")] { + pub const SOCK_CLOEXEC: c_int = 0x10000000; + } else { + pub const SOCK_CLOEXEC: c_int = O_CLOEXEC; + } +} pub const INET_ADDRSTRLEN: c_int = 16; @@ -690,7 +700,7 @@ pub const IFF_ALTPHYS: c_int = IFF_LINK2; // use alternate physical connection pub const IFF_MULTICAST: c_int = 0x8000; // supports multicast cfg_if! { - if #[cfg(target_os = "vita")] { + if #[cfg(any(target_os = "vita", target_os = "rtems"))] { pub const TCP_NODELAY: c_int = 1; pub const TCP_MAXSEG: c_int = 2; } else if #[cfg(target_os = "espidf")] { @@ -726,7 +736,7 @@ cfg_if! { } } cfg_if! { - if #[cfg(target_os = "vita")] { + if #[cfg(any(target_os = "vita", target_os = "rtems"))] { pub const IP_TTL: c_int = 4; } else if #[cfg(target_os = "espidf")] { pub const IP_TTL: c_int = 2; @@ -748,7 +758,7 @@ cfg_if! { } cfg_if! { - if #[cfg(target_os = "vita")] { + if #[cfg(any(target_os = "vita", target_os = "rtems"))] { pub const IP_ADD_MEMBERSHIP: c_int = 12; pub const IP_DROP_MEMBERSHIP: c_int = 13; } else if #[cfg(target_os = "espidf")] { @@ -783,6 +793,11 @@ cfg_if! { pub const NO_DATA: c_int = 211; pub const NO_RECOVERY: c_int = 212; pub const TRY_AGAIN: c_int = 213; + } else if #[cfg(target_os = "rtems")] { + pub const HOST_NOT_FOUND: c_int = 1; + pub const TRY_AGAIN: c_int = 2; + pub const NO_RECOVERY: c_int = 3; + pub const NO_DATA: c_int = 4; } else { pub const HOST_NOT_FOUND: c_int = 1; pub const NO_DATA: c_int = 2; @@ -790,7 +805,13 @@ cfg_if! { pub const TRY_AGAIN: c_int = 4; } } -pub const NO_ADDRESS: c_int = 2; +cfg_if! { + if #[cfg(target_os = "rtems")] { + pub const NO_ADDRESS: c_int = NO_DATA; + } else { + pub const NO_ADDRESS: c_int = 2; + } +} pub const AI_PASSIVE: c_int = 1; pub const AI_CANONNAME: c_int = 2; @@ -799,6 +820,9 @@ cfg_if! { if #[cfg(target_os = "espidf")] { pub const AI_NUMERICSERV: c_int = 8; pub const AI_ADDRCONFIG: c_int = 64; + } else if #[cfg(target_os = "rtems")] { + pub const AI_NUMERICSERV: c_int = 0x00000008; + pub const AI_ADDRCONFIG: c_int = 0x00000400; } else { pub const AI_NUMERICSERV: c_int = 0; pub const AI_ADDRCONFIG: c_int = 0; @@ -811,7 +835,7 @@ pub const NI_NOFQDN: c_int = 1; pub const NI_NUMERICHOST: c_int = 2; pub const NI_NAMEREQD: c_int = 4; cfg_if! { - if #[cfg(target_os = "espidf")] { + if #[cfg(any(target_os = "espidf", target_os = "rtems"))] { pub const NI_NUMERICSERV: c_int = 8; pub const NI_DGRAM: c_int = 16; } else { @@ -827,6 +851,11 @@ cfg_if! { pub const EAI_MEMORY: c_int = 203; pub const EAI_NONAME: c_int = 200; pub const EAI_SOCKTYPE: c_int = 10; + } else if #[cfg(target_os = "rtems")] { + pub const EAI_FAMILY: c_int = 5; + pub const EAI_MEMORY: c_int = 6; + pub const EAI_NONAME: c_int = 8; + pub const EAI_SOCKTYPE: c_int = 10; } else if #[cfg(not(target_os = "vita"))] { pub const EAI_FAMILY: c_int = -303; pub const EAI_MEMORY: c_int = -304; @@ -974,17 +1003,10 @@ cfg_if! { } else if #[cfg(target_os = "vita")] { mod vita; pub use self::vita::*; - } else if #[cfg(target_arch = "arm")] { - mod arm; - pub use self::arm::*; - } else { - core::compile_error!("unsupported target"); - } -} - -cfg_if! { - if #[cfg(target_os = "rtems")] { + } else if #[cfg(target_os = "rtems")] { mod rtems; pub use self::rtems::*; + } else { + core::compile_error!("unsupported target"); } } diff --git a/src/unix/newlib/rtems/mod.rs b/src/unix/newlib/rtems/mod.rs index 9ed423096e8db..ee98e91fdbe50 100644 --- a/src/unix/newlib/rtems/mod.rs +++ b/src/unix/newlib/rtems/mod.rs @@ -1,15 +1,68 @@ -// defined in architecture specific module - use crate::prelude::*; +pub type clock_t = c_long; +pub type wchar_t = u32; + s! { + pub struct sockaddr { + pub sa_len: u8, + pub sa_family: crate::sa_family_t, + pub sa_data: [c_char; 14], + } + + pub struct sockaddr_in { + pub sin_len: u8, + pub sin_family: crate::sa_family_t, + pub sin_port: crate::in_port_t, + pub sin_addr: crate::in_addr, + pub sin_zero: [c_char; 8], + } + + pub struct sockaddr_in6 { + pub sin6_len: u8, + pub sin6_family: crate::sa_family_t, + pub sin6_port: crate::in_port_t, + pub sin6_flowinfo: u32, + pub sin6_addr: crate::in6_addr, + pub sin6_scope_id: u32, + } + pub struct sockaddr_un { + pub sun_len: u8, pub sun_family: crate::sa_family_t, - pub sun_path: [c_char; 108usize], + pub sun_path: [c_char; 104usize], + } + + pub struct sockaddr_storage { + pub ss_len: u8, + pub ss_family: crate::sa_family_t, + __ss_pad1: Padding<[u8; 6]>, + __ss_align: i64, + __ss_pad2: Padding<[u8; 112]>, } } pub const AF_UNIX: c_int = 1; +pub const AF_INET6: c_int = 28; + +pub const FIONBIO: c_ulong = 0x8004667e; + +pub const POLLIN: c_short = 0x0001; +pub const POLLPRI: c_short = 0x0002; +pub const POLLOUT: c_short = 0x0004; +pub const POLLERR: c_short = 0x0008; +pub const POLLHUP: c_short = 0x0010; +pub const POLLNVAL: c_short = 0x0020; + +pub const SOL_SOCKET: c_int = 0xffff; + +pub const MSG_OOB: c_int = 0x1; +pub const MSG_PEEK: c_int = 0x2; +pub const MSG_DONTROUTE: c_int = 0x4; +pub const MSG_WAITALL: c_int = 0x40; +pub const MSG_DONTWAIT: c_int = 0x80; +pub const MSG_NOSIGNAL: c_int = 0x20000; +pub const MSG_MORE: c_int = 0; pub const RTLD_DEFAULT: *mut c_void = -2isize as *mut c_void;