Update.
[platform/upstream/glibc.git] / elf / dl-misc.c
1 /* Miscellaneous support functions for dynamic linker
2    Copyright (C) 1997 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #include <assert.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <sys/mman.h>
26 #include <sys/stat.h>
27
28 #ifndef MAP_ANON
29 /* This is the only dl-sysdep.c function that is actually needed at run-time
30    by _dl_map_object.  */
31
32 int
33 _dl_sysdep_open_zero_fill (void)
34 {
35   return __open ("/dev/zero", O_RDONLY);
36 }
37 #endif
38
39 /* Read the whole contents of FILE into new mmap'd space with given
40    protections.  *SIZEP gets the size of the file.  */
41
42 void *
43 _dl_sysdep_read_whole_file (const char *file, size_t *sizep, int prot)
44 {
45   void *result;
46   struct stat st;
47   int fd = __open (file, O_RDONLY);
48   if (fd < 0)
49     return NULL;
50   if (__fxstat (_STAT_VER, fd, &st) < 0)
51     result = NULL;
52   else
53     {
54       /* Map a copy of the file contents.  */
55       result = __mmap (0, st.st_size, prot,
56 #ifdef MAP_COPY
57                        MAP_COPY
58 #else
59                        MAP_PRIVATE
60 #endif
61 #ifdef MAP_FILE
62                        | MAP_FILE
63 #endif
64                        , fd, 0);
65       if (result == MAP_FAILED)
66         result = NULL;
67       else
68         *sizep = st.st_size;
69     }
70   __close (fd);
71   return result;
72 }
73
74
75 void
76 _dl_sysdep_fatal (const char *msg, ...)
77 {
78   va_list ap;
79
80   va_start (ap, msg);
81   do
82     {
83       size_t len = strlen (msg);
84       __write (STDERR_FILENO, msg, len);
85       msg = va_arg (ap, const char *);
86     } while (msg);
87   va_end (ap);
88
89   _exit (127);
90 }
91
92
93 void
94 _dl_sysdep_error (const char *msg, ...)
95 {
96   va_list ap;
97
98   va_start (ap, msg);
99   do
100     {
101       size_t len = strlen (msg);
102       __write (STDERR_FILENO, msg, len);
103       msg = va_arg (ap, const char *);
104     } while (msg);
105   va_end (ap);
106 }
107
108
109 void
110 _dl_sysdep_message (const char *msg, ...)
111 {
112   va_list ap;
113
114   va_start (ap, msg);
115   do
116     {
117       size_t len = strlen (msg);
118       __write (STDOUT_FILENO, msg, len);
119       msg = va_arg (ap, const char *);
120     } while (msg);
121   va_end (ap);
122 }