f80472485deef7bbe3dc343c900e62fff32a4b9d
[platform/upstream/glibc.git] / elf / dl-misc.c
1 /* Miscellaneous support functions for dynamic linker
2    Copyright (C) 1997,1998,1999,2000,2001,2002 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 Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the 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    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <assert.h>
21 #include <fcntl.h>
22 #include <ldsodefs.h>
23 #include <limits.h>
24 #include <link.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/mman.h>
30 #include <sys/param.h>
31 #include <sys/stat.h>
32 #include <sys/uio.h>
33 #include <stdio-common/_itoa.h>
34 #include <bits/libc-lock.h>
35
36 #ifndef MAP_ANON
37 /* This is the only dl-sysdep.c function that is actually needed at run-time
38    by _dl_map_object.  */
39
40 int
41 _dl_sysdep_open_zero_fill (void)
42 {
43   return __open ("/dev/zero", O_RDONLY);
44 }
45 #endif
46
47 /* Read the whole contents of FILE into new mmap'd space with given
48    protections.  *SIZEP gets the size of the file.  On error MAP_FAILED
49    is returned.  */
50
51 void *
52 internal_function
53 _dl_sysdep_read_whole_file (const char *file, size_t *sizep, int prot)
54 {
55   void *result = MAP_FAILED;
56   struct stat64 st;
57   int fd = __open (file, O_RDONLY);
58   if (fd >= 0)
59     {
60       if (__fxstat64 (_STAT_VER, fd, &st) >= 0)
61         {
62           *sizep = st.st_size;
63
64           /* No need to map the file if it is empty.  */
65           if (*sizep != 0)
66             /* Map a copy of the file contents.  */
67             result = __mmap (NULL, *sizep, prot,
68 #ifdef MAP_COPY
69                              MAP_COPY
70 #else
71                              MAP_PRIVATE
72 #endif
73 #ifdef MAP_FILE
74                              | MAP_FILE
75 #endif
76                              , fd, 0);
77         }
78       __close (fd);
79     }
80   return result;
81 }
82
83
84 /* Bare-bone printf implementation.  This function only knows about
85    the formats and flags needed and can handle only up to 64 stripes in
86    the output.  */
87 static void
88 _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
89 {
90   const int niovmax = 64;
91   struct iovec iov[niovmax];
92   int niov = 0;
93   pid_t pid = 0;
94   char pidbuf[7];
95
96   while (*fmt != '\0')
97     {
98       const char *startp = fmt;
99
100       if (tag_p > 0)
101         {
102           /* Generate the tag line once.  It consists of the PID and a
103              colon followed by a tab.  */
104           if (pid == 0)
105             {
106               char *p;
107               pid = __getpid ();
108               assert (pid >= 0 && pid < 100000);
109               p = _itoa (pid, &pidbuf[5], 10, 0);
110               while (p > pidbuf)
111                 *--p = '0';
112               pidbuf[5] = ':';
113               pidbuf[6] = '\t';
114             }
115
116           /* Append to the output.  */
117           assert (niov < niovmax);
118           iov[niov].iov_len = 7;
119           iov[niov++].iov_base = pidbuf;
120
121           /* No more tags until we see the next newline.  */
122           tag_p = -1;
123         }
124
125       /* Skip everything except % and \n (if tags are needed).  */
126       while (*fmt != '\0' && *fmt != '%' && (! tag_p || *fmt != '\n'))
127         ++fmt;
128
129       /* Append constant string.  */
130       assert (niov < niovmax);
131       if ((iov[niov].iov_len = fmt - startp) != 0)
132         iov[niov++].iov_base = (char *) startp;
133
134       if (*fmt == '%')
135         {
136           /* It is a format specifier.  */
137           char fill = ' ';
138           int width = -1;
139           int prec = -1;
140 #if LONG_MAX != INT_MAX
141           int long_mod = 0;
142 #endif
143
144           /* Recognize zero-digit fill flag.  */
145           if (*++fmt == '0')
146             {
147               fill = '0';
148               ++fmt;
149             }
150
151           /* See whether with comes from a parameter.  Note that no other
152              way to specify the width is implemented.  */
153           if (*fmt == '*')
154             {
155               width = va_arg (arg, int);
156               ++fmt;
157             }
158
159           /* Handle precision.  */
160           if (*fmt == '.' && fmt[1] == '*')
161             {
162               prec = va_arg (arg, int);
163               fmt += 2;
164             }
165
166           /* Recognize the l modifier.  It is only important on some
167              platforms where long and int have a different size.  We
168              can use the same code for size_t.  */
169           if (*fmt == 'l' || *fmt == 'Z')
170             {
171 #if LONG_MAX != INT_MAX
172               long_mod = 1;
173 #endif
174               ++fmt;
175             }
176
177           switch (*fmt)
178             {
179               /* Integer formatting.  */
180             case 'u':
181             case 'x':
182               {
183                 /* We have to make a difference if long and int have a
184                    different size.  */
185 #if LONG_MAX != INT_MAX
186                 unsigned long int num = (long_mod
187                                          ? va_arg (arg, unsigned long int)
188                                          : va_arg (arg, unsigned int));
189 #else
190                 unsigned long int num = va_arg (arg, unsigned int);
191 #endif
192                 /* We use alloca() to allocate the buffer with the most
193                    pessimistic guess for the size.  Using alloca() allows
194                    having more than one integer formatting in a call.  */
195                 char *buf = (char *) alloca (3 * sizeof (unsigned long int));
196                 char *endp = &buf[3 * sizeof (unsigned long int)];
197                 char *cp = _itoa (num, endp, *fmt == 'x' ? 16 : 10, 0);
198
199                 /* Pad to the width the user specified.  */
200                 if (width != -1)
201                   while (endp - cp < width)
202                     *--cp = fill;
203
204                 iov[niov].iov_base = cp;
205                 iov[niov].iov_len = endp - cp;
206                 ++niov;
207               }
208               break;
209
210             case 's':
211               /* Get the string argument.  */
212               iov[niov].iov_base = va_arg (arg, char *);
213               iov[niov].iov_len = strlen (iov[niov].iov_base);
214               if (prec != -1)
215                 iov[niov].iov_len = MIN ((size_t) prec, iov[niov].iov_len);
216               ++niov;
217               break;
218
219             case '%':
220               iov[niov].iov_base = (void *) fmt;
221               iov[niov].iov_len = 1;
222               ++niov;
223               break;
224
225             default:
226               assert (! "invalid format specifier");
227             }
228           ++fmt;
229         }
230       else if (*fmt == '\n')
231         {
232           /* See whether we have to print a single newline character.  */
233           if (fmt == startp)
234             {
235               iov[niov].iov_base = (char *) startp;
236               iov[niov++].iov_len = 1;
237             }
238           else
239             /* No, just add it to the rest of the string.  */
240             ++iov[niov - 1].iov_len;
241
242           /* Next line, print a tag again.  */
243           tag_p = 1;
244           ++fmt;
245         }
246     }
247
248   /* Finally write the result.  */
249 #ifdef INTERNAL_SYSCALL
250   INTERNAL_SYSCALL (writev, 3, fd, iov, niov);
251 #elif RTLD_PRIVATE_ERRNO
252   /* We have to take this lock just to be sure we don't clobber the private
253      errno when it's being used by another thread that cares about it.  */
254   __libc_lock_lock_recursive (GL(dl_load_lock));
255   __writev (fd, iov, niov);
256   __libc_lock_unlock_recursive (GL(dl_load_lock));
257 #else
258   __writev (fd, iov, niov);
259 #endif
260 }
261
262
263 /* Write to debug file.  */
264 void
265 _dl_debug_printf (const char *fmt, ...)
266 {
267   va_list arg;
268
269   va_start (arg, fmt);
270   _dl_debug_vdprintf (GL(dl_debug_fd), 1, fmt, arg);
271   va_end (arg);
272 }
273 INTDEF(_dl_debug_printf)
274
275
276 /* Write to debug file but don't start with a tag.  */
277 void
278 _dl_debug_printf_c (const char *fmt, ...)
279 {
280   va_list arg;
281
282   va_start (arg, fmt);
283   _dl_debug_vdprintf (GL(dl_debug_fd), -1, fmt, arg);
284   va_end (arg);
285 }
286
287
288 /* Write the given file descriptor.  */
289 void
290 _dl_dprintf (int fd, const char *fmt, ...)
291 {
292   va_list arg;
293
294   va_start (arg, fmt);
295   _dl_debug_vdprintf (fd, 0, fmt, arg);
296   va_end (arg);
297 }