Update.
[platform/upstream/glibc.git] / elf / dl-misc.c
1 /* Miscellaneous support functions for dynamic linker
2    Copyright (C) 1997, 1998 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 <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/stat.h>
28 #include <stdio-common/_itoa.h>
29
30 /* We have prototype anywhere.  */
31 extern ssize_t __libc_write __P ((int __fd, __const __ptr_t __buf,
32                                   size_t __n));
33
34 #ifndef MAP_ANON
35 /* This is the only dl-sysdep.c function that is actually needed at run-time
36    by _dl_map_object.  */
37
38 int
39 _dl_sysdep_open_zero_fill (void)
40 {
41   return __open ("/dev/zero", O_RDONLY);
42 }
43 #endif
44
45 /* Read the whole contents of FILE into new mmap'd space with given
46    protections.  *SIZEP gets the size of the file.  */
47
48 void *
49 _dl_sysdep_read_whole_file (const char *file, size_t *sizep, int prot)
50 {
51   void *result;
52   struct stat st;
53   int fd = __open (file, O_RDONLY);
54   if (fd < 0)
55     return NULL;
56   if (__fxstat (_STAT_VER, fd, &st) < 0
57       /* No need to map the file if it is empty.  */
58       || st.st_size == 0)
59     result = NULL;
60   else
61     {
62       /* Map a copy of the file contents.  */
63       result = __mmap (0, st.st_size, prot,
64 #ifdef MAP_COPY
65                        MAP_COPY
66 #else
67                        MAP_PRIVATE
68 #endif
69 #ifdef MAP_FILE
70                        | MAP_FILE
71 #endif
72                        , fd, 0);
73       if (result == MAP_FAILED)
74         result = NULL;
75       else
76         *sizep = st.st_size;
77     }
78   __close (fd);
79   return result;
80 }
81
82
83 /* Descriptor to write debug messages to.  */
84 int _dl_debug_fd = 2;
85
86
87 void
88 _dl_sysdep_output (int fd, const char *msg, ...)
89 {
90   va_list ap;
91
92   va_start (ap, msg);
93   do
94     {
95       size_t len = strlen (msg);
96       __libc_write (fd, msg, len);
97       msg = va_arg (ap, const char *);
98     }
99   while (msg != NULL);
100   va_end (ap);
101 }
102
103
104 void
105 _dl_debug_message (int new_line, const char *msg, ...)
106 {
107   /* We print the strings we get passed one after the other but start all
108      lines using the current PID.  */
109   static int pid;
110   va_list ap;
111
112   if (pid == 0)
113     pid = getpid ();
114
115   va_start (ap, msg);
116   do
117     if (msg[0] == '\0')
118       /* Get the next argument.  */
119       msg = va_arg (ap, const char *);
120     else
121       {
122         const char *endp;
123
124         /* We actually will print something in this line.  So print the
125            PID now if needed.  */
126         if (new_line)
127           {
128             char buf[7] = "00000:\t";
129             assert (pid >= 0 && pid < 100000);
130             _itoa_word (pid, &buf[5], 10, 0);
131             __libc_write (_dl_debug_fd, buf, 7);
132             new_line = 0;
133           }
134
135         endp = strchr (msg, '\n');
136         if (endp == NULL)
137           {
138             __libc_write (_dl_debug_fd, msg, strlen (msg));
139             msg = va_arg (ap, const char *);
140           }
141         else
142           {
143             __libc_write (_dl_debug_fd, msg, endp - msg + 1);
144             msg = endp + 1;
145             new_line = 1;
146           }
147       }
148   while (msg != NULL);
149   va_end (ap);
150 }