1 /* Minimal replacements for basic facilities used in the dynamic linker.
2 Copyright (C) 1995, 1996 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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, 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
;
36 extern int _dl_zerofd
;
38 if (_dl_pagesize
== 0)
39 _dl_pagesize
= __getpagesize ();
42 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
46 /* Consume any unused space in the last page of our data segment. */
49 alloc_end
= (void *) 0 + (((alloc_ptr
- (void *) 0) + _dl_pagesize
- 1)
50 & ~(_dl_pagesize
- 1));
53 /* Make sure the allocation pointer is ideally aligned. */
54 alloc_ptr
= (void *) 0 + (((alloc_ptr
- (void *) 0) + sizeof (double) - 1)
55 & ~(sizeof (double) - 1));
57 if (alloc_ptr
+ n
>= alloc_end
)
59 /* Insufficient space left; allocate another page. */
61 assert (n
<= _dl_pagesize
);
62 page
= __mmap (0, _dl_pagesize
, PROT_READ
|PROT_WRITE
,
63 MAP_ANON
|MAP_PRIVATE
, _dl_zerofd
, 0);
64 assert (page
!= (caddr_t
) -1);
65 if (page
!= alloc_end
)
67 alloc_end
= page
+ _dl_pagesize
;
70 alloc_last_block
= (void *) alloc_ptr
;
72 return alloc_last_block
;
76 /* This will rarely be called. */
80 /* We can free only the last block allocated. */
81 if (ptr
== alloc_last_block
)
82 alloc_ptr
= alloc_last_block
;
86 /* This is only called with the most recent block returned by malloc. */
88 realloc (void *ptr
, size_t n
)
91 assert (ptr
== alloc_last_block
);
92 alloc_ptr
= alloc_last_block
;
99 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
103 int __sigjmp_save (sigjmp_buf env
, int savemask
)
104 { env
[0].__mask_was_saved
= savemask
; return 0; }
105 weak_symbol (__sigjmp_save
)
108 longjmp (jmp_buf env
, int val
) { __longjmp (env
[0].__jmpbuf
, val
); }
109 weak_symbol (longjmp
)
112 /* Define our own stub for the localization function used by strerror.
113 English-only in the dynamic linker keeps it smaller. */
116 __dgettext (const char *domainname
, const char *msgid
)
118 assert (domainname
== _libc_intl_domainname
);
119 return (char *) msgid
;
121 weak_symbol (__dgettext
)
122 weak_alias (__dgettext
, dgettext
)
126 /* Define (weakly) our own assert failure function which doesn't use stdio.
127 If we are linked into the user program (-ldl), the normal __assert_fail
128 defn can override this one. */
131 __assert_fail (const char *assertion
,
132 const char *file
, unsigned int line
, const char *function
)
135 buf
[sizeof buf
- 1] = '\0';
136 _dl_sysdep_fatal ("BUG IN DYNAMIC LINKER ld.so: ",
137 file
, ": ", _itoa (line
, buf
+ sizeof buf
- 1, 10, 0),
138 ": ", function
?: "", function
? ": " : "",
139 "Assertion `", assertion
, "' failed!\n",
143 weak_symbol (__assert_fail
)
146 __assert_perror_fail (int errnum
,
147 const char *file
, unsigned int line
,
148 const char *function
)
151 buf
[sizeof buf
- 1] = '\0';
152 _dl_sysdep_fatal ("BUG IN DYNAMIC LINKER ld.so: ",
153 file
, ": ", _itoa (line
, buf
+ sizeof buf
- 1, 10, 0),
154 ": ", function
?: "", function
? ": " : "",
155 "Unexpected error: ", strerror (errnum
), "\n", NULL
);
158 weak_symbol (__assert_perror_fail
)