1 /* Minimal replacements for basic facilities used in the dynamic linker.
2 Copyright (C) 1995, 1996, 1997 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. */
20 #include <sys/types.h>
26 #include <stdio-common/_itoa.h>
28 /* Minimal `malloc' allocator for use while loading shared libraries.
29 Only small blocks are allocated, and none are ever freed. */
31 static void *alloc_ptr, *alloc_end, *alloc_last_block;
37 #define _dl_zerofd (-1)
39 extern int _dl_zerofd;
42 _dl_zerofd = _dl_sysdep_open_zero_fill ();
48 /* Consume any unused space in the last page of our data segment. */
51 alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0) + _dl_pagesize - 1)
52 & ~(_dl_pagesize - 1));
55 /* Make sure the allocation pointer is ideally aligned. */
56 alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + sizeof (double) - 1)
57 & ~(sizeof (double) - 1));
59 if (alloc_ptr + n >= alloc_end)
61 /* Insufficient space left; allocate another page. */
63 assert (n <= _dl_pagesize);
64 page = __mmap (0, _dl_pagesize, PROT_READ|PROT_WRITE,
65 MAP_ANON|MAP_PRIVATE, _dl_zerofd, 0);
66 assert (page != MAP_FAILED);
67 if (page != alloc_end)
69 alloc_end = page + _dl_pagesize;
72 alloc_last_block = (void *) alloc_ptr;
74 return alloc_last_block;
77 /* We use this function occasionally since the real implementation may
78 be optimized when it can assume the memory it returns already is
81 calloc (size_t nmemb, size_t size)
83 size_t total = nmemb * size;
84 void *result = malloc (total);
85 return memset (result, '\0', total);
88 /* This will rarely be called. */
92 /* We can free only the last block allocated. */
93 if (ptr == alloc_last_block)
94 alloc_ptr = alloc_last_block;
97 /* This is only called with the most recent block returned by malloc. */
99 realloc (void *ptr, size_t n)
102 assert (ptr == alloc_last_block);
103 alloc_ptr = alloc_last_block;
109 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
114 __sigjmp_save (sigjmp_buf env, int savemask)
116 env[0].__mask_was_saved = savemask;
121 longjmp (jmp_buf env, int val)
123 __longjmp (env[0].__jmpbuf, val);
126 /* Define our own stub for the localization function used by strerror.
127 English-only in the dynamic linker keeps it smaller. */
130 __dcgettext (const char *domainname, const char *msgid, int category)
132 return (char *) msgid;
134 weak_alias (__dcgettext, dcgettext)
138 /* Define (weakly) our own assert failure function which doesn't use stdio.
139 If we are linked into the user program (-ldl), the normal __assert_fail
140 defn can override this one. */
143 __assert_fail (const char *assertion,
144 const char *file, unsigned int line, const char *function)
147 buf[sizeof buf - 1] = '\0';
148 _dl_sysdep_fatal ("BUG IN DYNAMIC LINKER ld.so: ",
149 file, ": ", _itoa (line, buf + sizeof buf - 1, 10, 0),
150 ": ", function ?: "", function ? ": " : "",
151 "Assertion `", assertion, "' failed!\n",
157 __assert_perror_fail (int errnum,
158 const char *file, unsigned int line,
159 const char *function)
162 buf[sizeof buf - 1] = '\0';
163 _dl_sysdep_fatal ("BUG IN DYNAMIC LINKER ld.so: ",
164 file, ": ", _itoa (line, buf + sizeof buf - 1, 10, 0),
165 ": ", function ?: "", function ? ": " : "",
166 "Unexpected error: ", strerror (errnum), "\n", NULL);