1 /* Miscellaneous support functions for dynamic linker
2 Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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. */
30 #include <sys/param.h>
33 #include <stdio-common/_itoa.h>
36 /* This is the only dl-sysdep.c function that is actually needed at run-time
40 _dl_sysdep_open_zero_fill (void)
42 return __open ("/dev/zero", O_RDONLY
);
46 /* Read the whole contents of FILE into new mmap'd space with given
47 protections. *SIZEP gets the size of the file. */
51 _dl_sysdep_read_whole_file (const char *file
, size_t *sizep
, int prot
)
55 int fd
= __open (file
, O_RDONLY
);
58 if (__fxstat64 (_STAT_VER
, fd
, &st
) < 0
59 /* No need to map the file if it is empty. */
64 /* Map a copy of the file contents. */
65 result
= __mmap (0, st
.st_size
, prot
,
75 if (result
== MAP_FAILED
)
85 /* Descriptor to write debug messages to. */
89 /* Bare-bone printf implementation. This function only knows about
90 the formats and flags needed and can handle only up to 64 stripes in
93 _dl_debug_vdprintf (int fd
, int tag_p
, const char *fmt
, va_list arg
)
95 const int niovmax
= 64;
96 struct iovec iov
[niovmax
];
103 const char *startp
= fmt
;
107 /* Generate the tag line once. It consists of the PID and a
108 colon followed by a tab. */
113 assert (pid
>= 0 && pid
< 100000);
114 p
= _itoa_word (pid
, &pidbuf
[5], 10, 0);
121 /* Append to the output. */
122 assert (niov
< niovmax
);
123 iov
[niov
].iov_len
= 7;
124 iov
[niov
++].iov_base
= pidbuf
;
126 /* No more tags until we see the next newline. */
130 /* Skip everything except % and \n (if tags are needed). */
131 while (*fmt
!= '\0' && *fmt
!= '%' && (! tag_p
|| *fmt
!= '\n'))
134 /* Append constant string. */
135 assert (niov
< niovmax
);
136 if ((iov
[niov
].iov_len
= fmt
- startp
) != 0)
137 iov
[niov
++].iov_base
= (char *) startp
;
141 /* It is a format specifier. */
144 #if LONG_MAX != INT_MAX
148 /* Recognize zero-digit fill flag. */
155 /* See whether with comes from a parameter. Note that no other
156 way to specify the width is implemented. */
159 width
= va_arg (arg
, int);
163 /* Recognize the l modifier. It is only important on some
164 platforms where long and int have a different size. We
165 can use the same code for size_t. */
166 if (*fmt
== 'l' || *fmt
== 'Z')
168 #if LONG_MAX != INT_MAX
176 /* Integer formatting. */
180 /* We have to make a difference if long and int have a
182 #if LONG_MAX != INT_MAX
183 unsigned long int num
= (long_mod
184 ? va_arg (arg
, unsigned long int)
185 : va_arg (arg
, unsigned int));
187 unsigned long int num
= va_arg (arg
, unsigned int);
189 /* We use alloca() to allocate the buffer with the most
190 pessimistic guess for the size. Using alloca() allows
191 having more than one integer formatting in a call. */
192 char *buf
= (char *) alloca (3 * sizeof (unsigned long int));
193 char *endp
= &buf
[3 * sizeof (unsigned long int)];
194 char *cp
= _itoa_word (num
, endp
, *fmt
== 'x' ? 16 : 10, 0);
196 /* Pad to the width the user specified. */
198 while (endp
- cp
< width
)
201 iov
[niov
].iov_base
= cp
;
202 iov
[niov
].iov_len
= endp
- cp
;
208 /* Get the string argument. */
209 iov
[niov
].iov_base
= va_arg (arg
, char *);
210 iov
[niov
].iov_len
= strlen (iov
[niov
].iov_base
);
215 assert (! "invalid format specifier");
219 else if (*fmt
== '\n')
221 /* See whether we have to print a single newline character. */
224 iov
[niov
].iov_base
= (char *) startp
;
225 iov
[niov
++].iov_len
= 1;
228 /* No, just add it to the rest of the string. */
229 ++iov
[niov
- 1].iov_len
;
231 /* Next line, print a tag again. */
237 /* Finally write the result. */
238 __writev (fd
, iov
, niov
);
242 /* Write to debug file. */
244 _dl_debug_printf (const char *fmt
, ...)
249 _dl_debug_vdprintf (_dl_debug_fd
, 1, fmt
, arg
);
254 /* Write to debug file but don't start with a tag. */
256 _dl_debug_printf_c (const char *fmt
, ...)
261 _dl_debug_vdprintf (_dl_debug_fd
, -1, fmt
, arg
);
266 /* Write the given file descriptor. */
268 _dl_dprintf (int fd
, const char *fmt
, ...)
273 _dl_debug_vdprintf (fd
, 0, fmt
, arg
);