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