e354a5
commit b31d4355ae817aa3caf9414f842cc07465bca028
e354a5
Author: Florian Weimer <fweimer@redhat.com>
e354a5
Date:   Wed Oct 7 16:39:50 2020 +0200
e354a5
e354a5
    elf: Implement _dl_write
e354a5
    
e354a5
    The generic version is parallel to _dl_writev.  It cannot use
e354a5
    _dl_writev directly because the errno value needs to be obtained
e354a5
    under a lock.
e354a5
    
e354a5
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
e354a5
e354a5
Backport adjusted for different INTERNAL_SYSCALL_CALL definition
e354a5
downstream.
e354a5
e354a5
diff --git a/elf/Makefile b/elf/Makefile
e354a5
index ef655630d50b07aa..e2078f6bc325b7e0 100644
e354a5
--- a/elf/Makefile
e354a5
+++ b/elf/Makefile
e354a5
@@ -34,7 +34,7 @@ dl-routines	= $(addprefix dl-,load lookup object reloc deps \
e354a5
 				  version profile tls origin scope \
e354a5
 				  execstack open close trampoline \
e354a5
 				  exception sort-maps lookup-direct \
e354a5
-				  call-libc-early-init)
e354a5
+				  call-libc-early-init write)
e354a5
 ifeq (yes,$(use-ldconfig))
e354a5
 dl-routines += dl-cache
e354a5
 endif
e354a5
diff --git a/elf/dl-write.c b/elf/dl-write.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..7350aff0035d4fbc
e354a5
--- /dev/null
e354a5
+++ b/elf/dl-write.c
e354a5
@@ -0,0 +1,56 @@
e354a5
+/* Implementation of the _dl_write function.  Generic version.
e354a5
+   Copyright (C) 2020 Free Software Foundation, Inc.
e354a5
+   This file is part of the GNU C Library.
e354a5
+
e354a5
+   The GNU C Library is free software; you can redistribute it and/or
e354a5
+   modify it under the terms of the GNU Lesser General Public
e354a5
+   License as published by the Free Software Foundation; either
e354a5
+   version 2.1 of the License, or (at your option) any later version.
e354a5
+
e354a5
+   The GNU C Library is distributed in the hope that it will be useful,
e354a5
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
e354a5
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e354a5
+   Lesser General Public License for more details.
e354a5
+
e354a5
+   You should have received a copy of the GNU Lesser General Public
e354a5
+   License along with the GNU C Library; if not, see
e354a5
+   <https://www.gnu.org/licenses/>.  */
e354a5
+
e354a5
+#include <errno.h>
e354a5
+#include <ldsodefs.h>
e354a5
+#include <libc-lock.h>
e354a5
+#include <sys/uio.h>
e354a5
+
e354a5
+ssize_t
e354a5
+_dl_write (int fd, const void *buffer, size_t length)
e354a5
+{
e354a5
+  struct iovec iov = { .iov_base = (void *) buffer, .iov_len = length };
e354a5
+  ssize_t ret;
e354a5
+
e354a5
+#if RTLD_PRIVATE_ERRNO
e354a5
+  /* We have to take this lock just to be sure we don't clobber the private
e354a5
+     errno when it's being used by another thread that cares about it.
e354a5
+     Yet we must be sure not to try calling the lock functions before
e354a5
+     the thread library is fully initialized.  */
e354a5
+  if (__glibc_unlikely (_dl_starting_up))
e354a5
+    {
e354a5
+      ret = __writev (fd, &iov, 1);
e354a5
+      if (ret < 0)
e354a5
+        ret = -errno;
e354a5
+    }
e354a5
+  else
e354a5
+    {
e354a5
+      __rtld_lock_lock_recursive (GL(dl_load_lock));
e354a5
+      __writev (fd, &iov, 1);
e354a5
+      if (ret < 0)
e354a5
+        ret = -errno;
e354a5
+      __rtld_lock_unlock_recursive (GL(dl_load_lock));
e354a5
+    }
e354a5
+#else
e354a5
+  ret = __writev (fd, &iov, 1);
e354a5
+  if (ret < 0)
e354a5
+    ret = -errno;
e354a5
+#endif
e354a5
+
e354a5
+  return ret;
e354a5
+}
e354a5
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
e354a5
index 37f1915b0c75a020..4aa28b0229e0b339 100644
e354a5
--- a/sysdeps/generic/ldsodefs.h
e354a5
+++ b/sysdeps/generic/ldsodefs.h
e354a5
@@ -754,6 +754,12 @@ _dl_dprintf (int fd, const char *fmt, ...)
e354a5
 }
e354a5
 #endif
e354a5
 
e354a5
+/* Write LENGTH bytes at BUFFER to FD, like write.  Returns the number
e354a5
+   of bytes written on success, or a negative error constant on
e354a5
+   failure.  */
e354a5
+ssize_t _dl_write (int fd, const void *buffer, size_t length)
e354a5
+  attribute_hidden;
e354a5
+
e354a5
 /* Write a message on the specified descriptor standard output.  The
e354a5
    parameters are interpreted as for a `printf' call.  */
e354a5
 #define _dl_printf(fmt, args...) \
e354a5
diff --git a/sysdeps/unix/sysv/linux/dl-write.c b/sysdeps/unix/sysv/linux/dl-write.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..2c670a8059077076
e354a5
--- /dev/null
e354a5
+++ b/sysdeps/unix/sysv/linux/dl-write.c
e354a5
@@ -0,0 +1,31 @@
e354a5
+/* Implementation of the _dl_write function.  Linux version.
e354a5
+   Copyright (C) 2020 Free Software Foundation, Inc.
e354a5
+   This file is part of the GNU C Library.
e354a5
+
e354a5
+   The GNU C Library is free software; you can redistribute it and/or
e354a5
+   modify it under the terms of the GNU Lesser General Public
e354a5
+   License as published by the Free Software Foundation; either
e354a5
+   version 2.1 of the License, or (at your option) any later version.
e354a5
+
e354a5
+   The GNU C Library is distributed in the hope that it will be useful,
e354a5
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
e354a5
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e354a5
+   Lesser General Public License for more details.
e354a5
+
e354a5
+   You should have received a copy of the GNU Lesser General Public
e354a5
+   License along with the GNU C Library; if not, see
e354a5
+   <https://www.gnu.org/licenses/>.  */
e354a5
+
e354a5
+#include <sysdep.h>
e354a5
+#include <unistd.h>
e354a5
+#include <ldsodefs.h>
e354a5
+
e354a5
+ssize_t
e354a5
+_dl_write (int fd, const void *buffer, size_t length)
e354a5
+{
e354a5
+  INTERNAL_SYSCALL_DECL (err);
e354a5
+  long int r = INTERNAL_SYSCALL_CALL (write, err, fd, buffer, length);
e354a5
+  if (INTERNAL_SYSCALL_ERROR_P (r, err))
e354a5
+    r = - INTERNAL_SYSCALL_ERRNO (r, err);
e354a5
+  return r;
e354a5
+}