Add strerrordesc_np() and strerrorname_np().

strerrordesc_np() isn't very useful (being just another name for
strerror()), but strerrorname_np() lets you get "ENOSYS" for ENOSYS,
which will make some of our test assertion messages clearer when we
switch over from strerror().

This also adds `%#m` formatting to all the relevant functions.

Test: treehugger
Change-Id: Icfe07a39a307d591c3f4f2a09d008dc021643062
diff --git a/docs/status.md b/docs/status.md
index 3c5d1ba..3b4933b 100644
--- a/docs/status.md
+++ b/docs/status.md
@@ -63,12 +63,15 @@
     functions for avoiding $TZ if you need to use multiple timezones in
     multi-threaded C).
   * `mbsrtowcs_l` and `wcsrtombs_l` aliases for `mbsrtowcs` and `wcsrtombs`.
+  * GNU extensions `strerrordesc_np` and `strerrorname_np`.
   * New system call wrappers: `__riscv_flush_icache` (`<sys/cachectl.h>`),
     `__riscv_hwprobe` (`<sys/hwprobe.h>`), `epoll_pwait2`/`epoll_pwait2_64` (`<sys/epoll.h>`).
 
 New libc behavior in V (API level 35):
   * Added `LD_SHOW_AUXV` to the dynamic linker to dump the ELF auxiliary
     vector if the environment variable is set.
+  * The printf family now supports `%#m` to print the name of the errno
+    constant (rather than the description printed by `%m`).
 
 New libc functions in U (API level 34):
   * `close_range` and `copy_file_range` (Linux-specific GNU extensions).
diff --git a/libc/async_safe/async_safe_log.cpp b/libc/async_safe/async_safe_log.cpp
index 2bff616..451cb8c 100644
--- a/libc/async_safe/async_safe_log.cpp
+++ b/libc/async_safe/async_safe_log.cpp
@@ -361,7 +361,19 @@
       buffer[1] = 'x';
       format_integer(buffer + 2, sizeof(buffer) - 2, value, 'x');
     } else if (c == 'm') {
-      strerror_r(errno, buffer, sizeof(buffer));
+#if __ANDROID_API_LEVEL__ >= 35 // This library is used in mainline modules.
+      if (alternate) {
+        const char* name = strerrorname_np(errno);
+        if (name) {
+          strcpy(buffer, name);
+        } else {
+          format_integer(buffer, sizeof(buffer), errno, 'd');
+        }
+      } else
+#endif
+      {
+        strerror_r(errno, buffer, sizeof(buffer));
+      }
     } else if (tolower(c) == 'b' || c == 'd' || c == 'i' || c == 'o' || c == 'u' ||
                tolower(c) == 'x') {
       /* integers - first read value from stack */
diff --git a/libc/bionic/strerror.cpp b/libc/bionic/strerror.cpp
index 0deb200..743130d 100644
--- a/libc/bionic/strerror.cpp
+++ b/libc/bionic/strerror.cpp
@@ -26,8 +26,8 @@
  * SUCH DAMAGE.
  */
 
-// G++ automatically defines _GNU_SOURCE, which then means that <string.h>
-// gives us the GNU variant.
+// -std=gnu++XX automatically defines _GNU_SOURCE, which then means that <string.h>
+// gives us the GNU variant, which is not what we're defining here.
 #undef _GNU_SOURCE
 
 #include <string.h>
@@ -43,146 +43,28 @@
 
 #include "bionic/pthread_internal.h"
 
-static const char* __sys_error_strings[] = {
-    [0] = "Success",
-    [EPERM] = "Operation not permitted",
-    [ENOENT] = "No such file or directory",
-    [ESRCH] = "No such process",
-    [EINTR] = "Interrupted system call",
-    [EIO] = "I/O error",
-    [ENXIO] = "No such device or address",
-    [E2BIG] = "Argument list too long",
-    [ENOEXEC] = "Exec format error",
-    [EBADF] = "Bad file descriptor",
-    [ECHILD] = "No child processes",
-    [EAGAIN] = "Try again",
-    [ENOMEM] = "Out of memory",
-    [EACCES] = "Permission denied",
-    [EFAULT] = "Bad address",
-    [ENOTBLK] = "Block device required",
-    [EBUSY] = "Device or resource busy",
-    [EEXIST] = "File exists",
-    [EXDEV] = "Cross-device link",
-    [ENODEV] = "No such device",
-    [ENOTDIR] = "Not a directory",
-    [EISDIR] = "Is a directory",
-    [EINVAL] = "Invalid argument",
-    [ENFILE] = "File table overflow",
-    [EMFILE] = "Too many open files",
-    [ENOTTY] = "Inappropriate ioctl for device",
-    [ETXTBSY] = "Text file busy",
-    [EFBIG] = "File too large",
-    [ENOSPC] = "No space left on device",
-    [ESPIPE] = "Illegal seek",
-    [EROFS] = "Read-only file system",
-    [EMLINK] = "Too many links",
-    [EPIPE] = "Broken pipe",
-    [EDOM] = "Math argument out of domain of func",
-    [ERANGE] = "Math result not representable",
-    [EDEADLK] = "Resource deadlock would occur",
-    [ENAMETOOLONG] = "File name too long",
-    [ENOLCK] = "No record locks available",
-    [ENOSYS] = "Function not implemented",
-    [ENOTEMPTY] = "Directory not empty",
-    [ELOOP] = "Too many symbolic links encountered",
-    [ENOMSG] = "No message of desired type",
-    [EIDRM] = "Identifier removed",
-    [ECHRNG] = "Channel number out of range",
-    [EL2NSYNC] = "Level 2 not synchronized",
-    [EL3HLT] = "Level 3 halted",
-    [EL3RST] = "Level 3 reset",
-    [ELNRNG] = "Link number out of range",
-    [EUNATCH] = "Protocol driver not attached",
-    [ENOCSI] = "No CSI structure available",
-    [EL2HLT] = "Level 2 halted",
-    [EBADE] = "Invalid exchange",
-    [EBADR] = "Invalid request descriptor",
-    [EXFULL] = "Exchange full",
-    [ENOANO] = "No anode",
-    [EBADRQC] = "Invalid request code",
-    [EBADSLT] = "Invalid slot",
-    [EBFONT] = "Bad font file format",
-    [ENOSTR] = "Device not a stream",
-    [ENODATA] = "No data available",
-    [ETIME] = "Timer expired",
-    [ENOSR] = "Out of streams resources",
-    [ENONET] = "Machine is not on the network",
-    [ENOPKG] = "Package not installed",
-    [EREMOTE] = "Object is remote",
-    [ENOLINK] = "Link has been severed",
-    [EADV] = "Advertise error",
-    [ESRMNT] = "Srmount error",
-    [ECOMM] = "Communication error on send",
-    [EPROTO] = "Protocol error",
-    [EMULTIHOP] = "Multihop attempted",
-    [EDOTDOT] = "RFS specific error",
-    [EBADMSG] = "Not a data message",
-    [EOVERFLOW] = "Value too large for defined data type",
-    [ENOTUNIQ] = "Name not unique on network",
-    [EBADFD] = "File descriptor in bad state",
-    [EREMCHG] = "Remote address changed",
-    [ELIBACC] = "Can not access a needed shared library",
-    [ELIBBAD] = "Accessing a corrupted shared library",
-    [ELIBSCN] = ".lib section in a.out corrupted",
-    [ELIBMAX] = "Attempting to link in too many shared libraries",
-    [ELIBEXEC] = "Cannot exec a shared library directly",
-    [EILSEQ] = "Illegal byte sequence",
-    [ERESTART] = "Interrupted system call should be restarted",
-    [ESTRPIPE] = "Streams pipe error",
-    [EUSERS] = "Too many users",
-    [ENOTSOCK] = "Socket operation on non-socket",
-    [EDESTADDRREQ] = "Destination address required",
-    [EMSGSIZE] = "Message too long",
-    [EPROTOTYPE] = "Protocol wrong type for socket",
-    [ENOPROTOOPT] = "Protocol not available",
-    [EPROTONOSUPPORT] = "Protocol not supported",
-    [ESOCKTNOSUPPORT] = "Socket type not supported",
-    [EOPNOTSUPP] = "Operation not supported on transport endpoint",
-    [EPFNOSUPPORT] = "Protocol family not supported",
-    [EAFNOSUPPORT] = "Address family not supported by protocol",
-    [EADDRINUSE] = "Address already in use",
-    [EADDRNOTAVAIL] = "Cannot assign requested address",
-    [ENETDOWN] = "Network is down",
-    [ENETUNREACH] = "Network is unreachable",
-    [ENETRESET] = "Network dropped connection because of reset",
-    [ECONNABORTED] = "Software caused connection abort",
-    [ECONNRESET] = "Connection reset by peer",
-    [ENOBUFS] = "No buffer space available",
-    [EISCONN] = "Transport endpoint is already connected",
-    [ENOTCONN] = "Transport endpoint is not connected",
-    [ESHUTDOWN] = "Cannot send after transport endpoint shutdown",
-    [ETOOMANYREFS] = "Too many references: cannot splice",
-    [ETIMEDOUT] = "Connection timed out",
-    [ECONNREFUSED] = "Connection refused",
-    [EHOSTDOWN] = "Host is down",
-    [EHOSTUNREACH] = "No route to host",
-    [EALREADY] = "Operation already in progress",
-    [EINPROGRESS] = "Operation now in progress",
-    [ESTALE] = "Stale NFS file handle",
-    [EUCLEAN] = "Structure needs cleaning",
-    [ENOTNAM] = "Not a XENIX named type file",
-    [ENAVAIL] = "No XENIX semaphores available",
-    [EISNAM] = "Is a named type file",
-    [EREMOTEIO] = "Remote I/O error",
-    [EDQUOT] = "Quota exceeded",
-    [ENOMEDIUM] = "No medium found",
-    [EMEDIUMTYPE] = "Wrong medium type",
-    [ECANCELED] = "Operation Canceled",
-    [ENOKEY] = "Required key not available",
-    [EKEYEXPIRED] = "Key has expired",
-    [EKEYREVOKED] = "Key has been revoked",
-    [EKEYREJECTED] = "Key was rejected by service",
-    [EOWNERDEAD] = "Owner died",
-    [ENOTRECOVERABLE] = "State not recoverable",
-    [ERFKILL] = "Operation not possible due to RF-kill",
-    [EHWPOISON] = "Memory page has hardware error",
+static const char* __sys_error_descriptions[] = {
+#define __BIONIC_ERRDEF(error_number, error_description) [error_number] = error_description,
+#include "private/bionic_errdefs.h"
 };
 
-static inline const char* __strerror_lookup(int error_number) {
-  if (error_number < 0 || error_number >= static_cast<int>(arraysize(__sys_error_strings))) {
+static const char* __sys_error_names[] = {
+#define __BIONIC_ERRDEF(error_number, error_description) [error_number] = #error_number,
+#include "private/bionic_errdefs.h"
+};
+
+extern "C" const char* strerrorname_np(int error_number) {
+  if (error_number < 0 || error_number >= static_cast<int>(arraysize(__sys_error_names))) {
     return nullptr;
   }
-  return __sys_error_strings[error_number];
+  return __sys_error_names[error_number];
+}
+
+static inline const char* __strerror_lookup(int error_number) {
+  if (error_number < 0 || error_number >= static_cast<int>(arraysize(__sys_error_descriptions))) {
+    return nullptr;
+  }
+  return __sys_error_descriptions[error_number];
 }
 
 int strerror_r(int error_number, char* buf, size_t buf_len) {
diff --git a/libc/bionic/strsignal.cpp b/libc/bionic/strsignal.cpp
index 05d3498..f18b6d0 100644
--- a/libc/bionic/strsignal.cpp
+++ b/libc/bionic/strsignal.cpp
@@ -32,12 +32,12 @@
 #include "bionic/pthread_internal.h"
 
 const char* const sys_siglist[NSIG] = {
-#define __BIONIC_SIGDEF(signal_number, signal_description) [ signal_number ] = signal_description,
+#define __BIONIC_SIGDEF(signal_number, signal_description) [signal_number] = signal_description,
 #include "private/bionic_sigdefs.h"
 };
 
 const char* const sys_signame[NSIG] = {
-#define __BIONIC_SIGDEF(signal_number, unused) [ signal_number ] = &(#signal_number)[3],
+#define __BIONIC_SIGDEF(signal_number, unused) [signal_number] = &(#signal_number)[3],
 #include "private/bionic_sigdefs.h"
 };
 
diff --git a/libc/include/string.h b/libc/include/string.h
index e7fd9a5..89b2a45 100644
--- a/libc/include/string.h
+++ b/libc/include/string.h
@@ -116,6 +116,33 @@
 int strerror_r(int __errno_value, char* _Nonnull __buf, size_t __n);
 #endif
 
+/**
+ * [strerrorname_np(3)](http://man7.org/linux/man-pages/man3/strerrordesc_np.3.html)
+ * returns the name of the errno constant corresponding to its argument.
+ * `strerrorname_np(38)` would return "ENOSYS", because `ENOSYS` is errno 38. This
+ * is mostly useful for error reporting in cases where a string like "ENOSYS" is
+ * more readable than a string like "Function not implemented", which would be
+ * returned by strerror().
+ *
+ * Returns a pointer to a string, or null for unknown errno values.
+ *
+ * Available since API level 35.
+ */
+#if defined(__USE_GNU)
+const char* _Nullable strerrorname_np(int __errno_value) __INTRODUCED_IN(35);
+#endif
+
+/**
+ * [strerrordesc_np(3)](http://man7.org/linux/man-pages/man3/strerrordesc_np.3.html)
+ * is like strerror() but without localization. Since Android's strerror()
+ * does not localize, this is the same as strerror() on Android.
+ *
+ * Returns a pointer to a string.
+ */
+#if defined(__USE_GNU)
+const char* _Nonnull strerrordesc_np(int __errno_value) __RENAME(strerror);
+#endif
+
 size_t strnlen(const char* _Nonnull __s, size_t __n) __attribute_pure__;
 char* _Nonnull strncat(char* _Nonnull __dst, const char* _Nonnull __src, size_t __n);
 char* _Nullable strndup(const char* _Nonnull __s, size_t __n);
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
index e6ea3c2..824682b 100644
--- a/libc/libc.map.txt
+++ b/libc/libc.map.txt
@@ -1593,6 +1593,7 @@
     mktime_z;
     __riscv_flush_icache; # riscv64
     __riscv_hwprobe; # riscv64
+    strerrorname_np;
     tcgetwinsize;
     tcsetwinsize;
     timespec_getres;
diff --git a/libc/private/bionic_errdefs.h b/libc/private/bionic_errdefs.h
new file mode 100644
index 0000000..435d49b
--- /dev/null
+++ b/libc/private/bionic_errdefs.h
@@ -0,0 +1,171 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * This header is used to define error constants and names;
+ * it might be included several times.
+ */
+
+#ifndef __BIONIC_ERRDEF
+#error __BIONIC_ERRDEF not defined
+#endif
+
+__BIONIC_ERRDEF(0, "Success")
+__BIONIC_ERRDEF(EPERM, "Operation not permitted")
+__BIONIC_ERRDEF(ENOENT, "No such file or directory")
+__BIONIC_ERRDEF(ESRCH, "No such process")
+__BIONIC_ERRDEF(EINTR, "Interrupted system call")
+__BIONIC_ERRDEF(EIO, "I/O error")
+__BIONIC_ERRDEF(ENXIO, "No such device or address")
+__BIONIC_ERRDEF(E2BIG, "Argument list too long")
+__BIONIC_ERRDEF(ENOEXEC, "Exec format error")
+__BIONIC_ERRDEF(EBADF, "Bad file descriptor")
+__BIONIC_ERRDEF(ECHILD, "No child processes")
+__BIONIC_ERRDEF(EAGAIN, "Try again")
+__BIONIC_ERRDEF(ENOMEM, "Out of memory")
+__BIONIC_ERRDEF(EACCES, "Permission denied")
+__BIONIC_ERRDEF(EFAULT, "Bad address")
+__BIONIC_ERRDEF(ENOTBLK, "Block device required")
+__BIONIC_ERRDEF(EBUSY, "Device or resource busy")
+__BIONIC_ERRDEF(EEXIST, "File exists")
+__BIONIC_ERRDEF(EXDEV, "Cross-device link")
+__BIONIC_ERRDEF(ENODEV, "No such device")
+__BIONIC_ERRDEF(ENOTDIR, "Not a directory")
+__BIONIC_ERRDEF(EISDIR, "Is a directory")
+__BIONIC_ERRDEF(EINVAL, "Invalid argument")
+__BIONIC_ERRDEF(ENFILE, "File table overflow")
+__BIONIC_ERRDEF(EMFILE, "Too many open files")
+__BIONIC_ERRDEF(ENOTTY, "Inappropriate ioctl for device")
+__BIONIC_ERRDEF(ETXTBSY, "Text file busy")
+__BIONIC_ERRDEF(EFBIG, "File too large")
+__BIONIC_ERRDEF(ENOSPC, "No space left on device")
+__BIONIC_ERRDEF(ESPIPE, "Illegal seek")
+__BIONIC_ERRDEF(EROFS, "Read-only file system")
+__BIONIC_ERRDEF(EMLINK, "Too many links")
+__BIONIC_ERRDEF(EPIPE, "Broken pipe")
+__BIONIC_ERRDEF(EDOM, "Math argument out of domain of func")
+__BIONIC_ERRDEF(ERANGE, "Math result not representable")
+__BIONIC_ERRDEF(EDEADLK, "Resource deadlock would occur")
+__BIONIC_ERRDEF(ENAMETOOLONG, "File name too long")
+__BIONIC_ERRDEF(ENOLCK, "No record locks available")
+__BIONIC_ERRDEF(ENOSYS, "Function not implemented")
+__BIONIC_ERRDEF(ENOTEMPTY, "Directory not empty")
+__BIONIC_ERRDEF(ELOOP, "Too many symbolic links encountered")
+__BIONIC_ERRDEF(ENOMSG, "No message of desired type")
+__BIONIC_ERRDEF(EIDRM, "Identifier removed")
+__BIONIC_ERRDEF(ECHRNG, "Channel number out of range")
+__BIONIC_ERRDEF(EL2NSYNC, "Level 2 not synchronized")
+__BIONIC_ERRDEF(EL3HLT, "Level 3 halted")
+__BIONIC_ERRDEF(EL3RST, "Level 3 reset")
+__BIONIC_ERRDEF(ELNRNG, "Link number out of range")
+__BIONIC_ERRDEF(EUNATCH, "Protocol driver not attached")
+__BIONIC_ERRDEF(ENOCSI, "No CSI structure available")
+__BIONIC_ERRDEF(EL2HLT, "Level 2 halted")
+__BIONIC_ERRDEF(EBADE, "Invalid exchange")
+__BIONIC_ERRDEF(EBADR, "Invalid request descriptor")
+__BIONIC_ERRDEF(EXFULL, "Exchange full")
+__BIONIC_ERRDEF(ENOANO, "No anode")
+__BIONIC_ERRDEF(EBADRQC, "Invalid request code")
+__BIONIC_ERRDEF(EBADSLT, "Invalid slot")
+__BIONIC_ERRDEF(EBFONT, "Bad font file format")
+__BIONIC_ERRDEF(ENOSTR, "Device not a stream")
+__BIONIC_ERRDEF(ENODATA, "No data available")
+__BIONIC_ERRDEF(ETIME, "Timer expired")
+__BIONIC_ERRDEF(ENOSR, "Out of streams resources")
+__BIONIC_ERRDEF(ENONET, "Machine is not on the network")
+__BIONIC_ERRDEF(ENOPKG, "Package not installed")
+__BIONIC_ERRDEF(EREMOTE, "Object is remote")
+__BIONIC_ERRDEF(ENOLINK, "Link has been severed")
+__BIONIC_ERRDEF(EADV, "Advertise error")
+__BIONIC_ERRDEF(ESRMNT, "Srmount error")
+__BIONIC_ERRDEF(ECOMM, "Communication error on send")
+__BIONIC_ERRDEF(EPROTO, "Protocol error")
+__BIONIC_ERRDEF(EMULTIHOP, "Multihop attempted")
+__BIONIC_ERRDEF(EDOTDOT, "RFS specific error")
+__BIONIC_ERRDEF(EBADMSG, "Not a data message")
+__BIONIC_ERRDEF(EOVERFLOW, "Value too large for defined data type")
+__BIONIC_ERRDEF(ENOTUNIQ, "Name not unique on network")
+__BIONIC_ERRDEF(EBADFD, "File descriptor in bad state")
+__BIONIC_ERRDEF(EREMCHG, "Remote address changed")
+__BIONIC_ERRDEF(ELIBACC, "Can not access a needed shared library")
+__BIONIC_ERRDEF(ELIBBAD, "Accessing a corrupted shared library")
+__BIONIC_ERRDEF(ELIBSCN, ".lib section in a.out corrupted")
+__BIONIC_ERRDEF(ELIBMAX, "Attempting to link in too many shared libraries")
+__BIONIC_ERRDEF(ELIBEXEC, "Cannot exec a shared library directly")
+__BIONIC_ERRDEF(EILSEQ, "Illegal byte sequence")
+__BIONIC_ERRDEF(ERESTART, "Interrupted system call should be restarted")
+__BIONIC_ERRDEF(ESTRPIPE, "Streams pipe error")
+__BIONIC_ERRDEF(EUSERS, "Too many users")
+__BIONIC_ERRDEF(ENOTSOCK, "Socket operation on non-socket")
+__BIONIC_ERRDEF(EDESTADDRREQ, "Destination address required")
+__BIONIC_ERRDEF(EMSGSIZE, "Message too long")
+__BIONIC_ERRDEF(EPROTOTYPE, "Protocol wrong type for socket")
+__BIONIC_ERRDEF(ENOPROTOOPT, "Protocol not available")
+__BIONIC_ERRDEF(EPROTONOSUPPORT, "Protocol not supported")
+__BIONIC_ERRDEF(ESOCKTNOSUPPORT, "Socket type not supported")
+__BIONIC_ERRDEF(EOPNOTSUPP, "Operation not supported on transport endpoint")
+__BIONIC_ERRDEF(EPFNOSUPPORT, "Protocol family not supported")
+__BIONIC_ERRDEF(EAFNOSUPPORT, "Address family not supported by protocol")
+__BIONIC_ERRDEF(EADDRINUSE, "Address already in use")
+__BIONIC_ERRDEF(EADDRNOTAVAIL, "Cannot assign requested address")
+__BIONIC_ERRDEF(ENETDOWN, "Network is down")
+__BIONIC_ERRDEF(ENETUNREACH, "Network is unreachable")
+__BIONIC_ERRDEF(ENETRESET, "Network dropped connection because of reset")
+__BIONIC_ERRDEF(ECONNABORTED, "Software caused connection abort")
+__BIONIC_ERRDEF(ECONNRESET, "Connection reset by peer")
+__BIONIC_ERRDEF(ENOBUFS, "No buffer space available")
+__BIONIC_ERRDEF(EISCONN, "Transport endpoint is already connected")
+__BIONIC_ERRDEF(ENOTCONN, "Transport endpoint is not connected")
+__BIONIC_ERRDEF(ESHUTDOWN, "Cannot send after transport endpoint shutdown")
+__BIONIC_ERRDEF(ETOOMANYREFS, "Too many references: cannot splice")
+__BIONIC_ERRDEF(ETIMEDOUT, "Connection timed out")
+__BIONIC_ERRDEF(ECONNREFUSED, "Connection refused")
+__BIONIC_ERRDEF(EHOSTDOWN, "Host is down")
+__BIONIC_ERRDEF(EHOSTUNREACH, "No route to host")
+__BIONIC_ERRDEF(EALREADY, "Operation already in progress")
+__BIONIC_ERRDEF(EINPROGRESS, "Operation now in progress")
+__BIONIC_ERRDEF(ESTALE, "Stale NFS file handle")
+__BIONIC_ERRDEF(EUCLEAN, "Structure needs cleaning")
+__BIONIC_ERRDEF(ENOTNAM, "Not a XENIX named type file")
+__BIONIC_ERRDEF(ENAVAIL, "No XENIX semaphores available")
+__BIONIC_ERRDEF(EISNAM, "Is a named type file")
+__BIONIC_ERRDEF(EREMOTEIO, "Remote I/O error")
+__BIONIC_ERRDEF(EDQUOT, "Quota exceeded")
+__BIONIC_ERRDEF(ENOMEDIUM, "No medium found")
+__BIONIC_ERRDEF(EMEDIUMTYPE, "Wrong medium type")
+__BIONIC_ERRDEF(ECANCELED, "Operation Canceled")
+__BIONIC_ERRDEF(ENOKEY, "Required key not available")
+__BIONIC_ERRDEF(EKEYEXPIRED, "Key has expired")
+__BIONIC_ERRDEF(EKEYREVOKED, "Key has been revoked")
+__BIONIC_ERRDEF(EKEYREJECTED, "Key was rejected by service")
+__BIONIC_ERRDEF(EOWNERDEAD, "Owner died")
+__BIONIC_ERRDEF(ENOTRECOVERABLE, "State not recoverable")
+__BIONIC_ERRDEF(ERFKILL, "Operation not possible due to RF-kill")
+__BIONIC_ERRDEF(EHWPOISON, "Memory page has hardware error")
+
+#undef __BIONIC_ERRDEF
diff --git a/libc/private/bionic_sigdefs.h b/libc/private/bionic_sigdefs.h
index 44d60d9..7e1b477 100644
--- a/libc/private/bionic_sigdefs.h
+++ b/libc/private/bionic_sigdefs.h
@@ -27,8 +27,8 @@
  */
 
 /*
- * this header is used to define signal constants and names;
- * it might be included several times
+ * This header is used to define signal constants and names;
+ * it might be included several times.
  */
 
 #ifndef __BIONIC_SIGDEF
@@ -41,9 +41,6 @@
 __BIONIC_SIGDEF(SIGILL,    "Illegal instruction")
 __BIONIC_SIGDEF(SIGTRAP,   "Trap")
 __BIONIC_SIGDEF(SIGABRT,   "Aborted")
-#ifdef SIGEMT
-__BIONIC_SIGDEF(SIGEMT,    "EMT")
-#endif
 __BIONIC_SIGDEF(SIGFPE,    "Floating point exception")
 __BIONIC_SIGDEF(SIGKILL,   "Killed")
 __BIONIC_SIGDEF(SIGBUS,    "Bus error")
@@ -67,9 +64,7 @@
 __BIONIC_SIGDEF(SIGPROF,   "Profiling timer expired")
 __BIONIC_SIGDEF(SIGXCPU,   "CPU time limit exceeded")
 __BIONIC_SIGDEF(SIGXFSZ,   "File size limit exceeded")
-#if defined(SIGSTKFLT)
 __BIONIC_SIGDEF(SIGSTKFLT, "Stack fault")
-#endif
 __BIONIC_SIGDEF(SIGSYS,    "Bad system call")
 
 #undef __BIONIC_SIGDEF
diff --git a/libc/stdio/printf_common.h b/libc/stdio/printf_common.h
index 365728b..702f8d3 100644
--- a/libc/stdio/printf_common.h
+++ b/libc/stdio/printf_common.h
@@ -786,7 +786,7 @@
   //
   // Returns NULL on failure.
   // To find out what happened check errno for ENOMEM, EILSEQ and EINVAL.
-  static wchar_t* mbsconv(char* mbsarg, int prec) {
+  static wchar_t* mbsconv(const char* mbsarg, int prec) {
     mbstate_t mbs;
     const char* p;
     size_t insize, nchars, nconv;
diff --git a/libc/stdio/vfprintf.cpp b/libc/stdio/vfprintf.cpp
index 12cceeb..e0509aa 100644
--- a/libc/stdio/vfprintf.cpp
+++ b/libc/stdio/vfprintf.cpp
@@ -340,6 +340,7 @@
       case 'd':
       case 'i':
         _umax = SARG();
+signed_decimal:
         if ((intmax_t)_umax < 0) {
           _umax = -_umax;
           sign = '-';
@@ -468,7 +469,15 @@
       case 'n':
         __fortify_fatal("%%n not allowed on Android");
       case 'm':
-        cp = strerror_r(caller_errno, buf, sizeof(buf));
+        if (flags & ALT) {
+          cp = const_cast<char*>(strerrorname_np(caller_errno));
+          if (cp == nullptr) {
+            _umax = caller_errno;
+            goto signed_decimal;
+          }
+        } else {
+          cp = strerror_r(caller_errno, buf, sizeof(buf));
+        }
         goto string;
       case 'O':
         flags |= LONGINT;
diff --git a/libc/stdio/vfwprintf.cpp b/libc/stdio/vfwprintf.cpp
index d6f6a6b..72f973c 100644
--- a/libc/stdio/vfwprintf.cpp
+++ b/libc/stdio/vfwprintf.cpp
@@ -52,6 +52,19 @@
 
 #include "printf_common.h"
 
+#define print_utf8(utf8, prec) \
+  do { \
+    free(convbuf); \
+    convbuf = helpers::mbsconv(utf8, prec); \
+    if (convbuf == nullptr) { \
+      fp->_flags |= __SERR; \
+      goto error; \
+    } else { \
+      cp = convbuf; \
+    } \
+    goto string; \
+  } while (0)
+
 int FUNCTION_NAME(FILE* fp, const CHAR_TYPE* fmt0, va_list ap) {
   int caller_errno = errno;
   int n, n2;
@@ -319,6 +332,7 @@
       case 'd':
       case 'i':
         _umax = SARG();
+signed_decimal:
         if ((intmax_t)_umax < 0) {
           _umax = -_umax;
           sign = '-';
@@ -447,16 +461,13 @@
       case 'n':
         __fortify_fatal("%%n not allowed on Android");
       case 'm':
-        free(convbuf);
-        convbuf = helpers::mbsconv(strerror_r(caller_errno,
-                                              reinterpret_cast<char*>(buf), sizeof(buf)), prec);
-        if (convbuf == nullptr) {
-            fp->_flags |= __SERR;
-            goto error;
-        } else {
-            cp = convbuf;
+        if (flags & ALT) {
+          const char* name = strerrorname_np(caller_errno);
+          if (name) print_utf8(name, prec);
+          _umax = caller_errno;
+          goto signed_decimal;
         }
-        goto string;
+        print_utf8(strerror_r(caller_errno, reinterpret_cast<char*>(buf), sizeof(buf)), prec);
       case 'O':
         flags |= LONGINT;
         __BIONIC_FALLTHROUGH;
@@ -486,14 +497,7 @@
         } else {
           char* mbsarg;
           if ((mbsarg = GETARG(char*)) == nullptr) mbsarg = const_cast<char*>("(null)");
-          free(convbuf);
-          convbuf = helpers::mbsconv(mbsarg, prec);
-          if (convbuf == nullptr) {
-            fp->_flags |= __SERR;
-            goto error;
-          } else {
-            cp = convbuf;
-          }
+          print_utf8(mbsarg, prec);
         }
   string:
         if (prec >= 0) {
diff --git a/tests/async_safe_test.cpp b/tests/async_safe_test.cpp
index cc1b598..ffb8651 100644
--- a/tests/async_safe_test.cpp
+++ b/tests/async_safe_test.cpp
@@ -79,6 +79,18 @@
   async_safe_format_buffer(buf, sizeof(buf), "a%mZ");
   EXPECT_STREQ("aInvalid argumentZ", buf);
 
+#if __ANDROID_API_LEVEL__ >= 35
+  errno = EINVAL;
+  async_safe_format_buffer(buf, sizeof(buf), "a%#mZ");
+  EXPECT_STREQ("aEINVALZ", buf);
+#endif
+
+#if __ANDROID_API_LEVEL__ >= 35
+  errno = -1;
+  async_safe_format_buffer(buf, sizeof(buf), "a%#mZ");
+  EXPECT_STREQ("a-1Z", buf);
+#endif
+
   async_safe_format_buffer(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
   EXPECT_STREQ("a0xb0001234b", buf);
 
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp
index b0f59bb..eaacc42 100644
--- a/tests/stdio_test.cpp
+++ b/tests/stdio_test.cpp
@@ -2523,6 +2523,24 @@
   EXPECT_SWPRINTF(L"<Invalid argument>", L"<%m>");
 }
 
+TEST(STDIO_TEST, printf_hash_m) {
+  errno = 0;
+  EXPECT_SNPRINTF("<0>", "<%#m>");
+  errno = -1;
+  EXPECT_SNPRINTF("<-1>", "<%#m>");
+  errno = EINVAL;
+  EXPECT_SNPRINTF("<EINVAL>", "<%#m>");
+}
+
+TEST(STDIO_TEST, wprintf_hash_m) {
+  errno = 0;
+  EXPECT_SWPRINTF(L"<0>", L"<%#m>");
+  errno = -1;
+  EXPECT_SWPRINTF(L"<-1>", L"<%#m>");
+  errno = EINVAL;
+  EXPECT_SWPRINTF(L"<EINVAL>", L"<%#m>");
+}
+
 TEST(STDIO_TEST, printf_m_does_not_clobber_strerror) {
   const char* m = strerror(-1);
   ASSERT_STREQ("Unknown error -1", m);
diff --git a/tests/string_test.cpp b/tests/string_test.cpp
index 38957e2..e7e0f34 100644
--- a/tests/string_test.cpp
+++ b/tests/string_test.cpp
@@ -1685,3 +1685,16 @@
   GTEST_SKIP() << "memset_explicit not available";
 #endif
 }
+
+TEST(STRING_TEST, strerrorname_np) {
+#if defined(__BIONIC__)
+  ASSERT_STREQ("0", strerrorname_np(0));
+  ASSERT_STREQ("EINVAL", strerrorname_np(EINVAL));
+  ASSERT_STREQ("ENOSYS", strerrorname_np(ENOSYS));
+
+  ASSERT_EQ(nullptr, strerrorname_np(-1));
+  ASSERT_EQ(nullptr, strerrorname_np(666));
+#else
+  GTEST_SKIP() << "strerrorname_np not available";
+#endif
+}