Mon May 13 12:03:03 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / elf / dl-minimal.c
blob9e0a77032d125546ebf9beaee77c97d978fce625
1 /* Minimal replacements for basic facilities used in the dynamic linker.
2 Copyright (C) 1995 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>
21 #include <sys/mman.h>
22 #include <unistd.h>
23 #include <assert.h>
24 #include <string.h>
25 #include <link.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;
33 void *
34 malloc (size_t n)
36 extern int _dl_zerofd;
37 static size_t pagesize;
39 if (pagesize == 0)
40 pagesize = __getpagesize ();
42 if (_dl_zerofd == -1)
43 _dl_zerofd = _dl_sysdep_open_zero_fill ();
45 if (alloc_end == 0)
47 /* Consume any unused space in the last page of our data segment. */
48 extern int _end;
49 alloc_ptr = &_end;
50 alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0) + pagesize - 1)
51 & ~(pagesize - 1));
54 /* Make sure the allocation pointer is ideally aligned. */
55 alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + sizeof (double) - 1)
56 & ~(sizeof (double) - 1));
58 if (alloc_ptr + n >= alloc_end)
60 /* Insufficient space left; allocate another page. */
61 caddr_t page;
62 assert (n <= pagesize);
63 page = mmap (0, pagesize, PROT_READ|PROT_WRITE,
64 MAP_ANON|MAP_PRIVATE, _dl_zerofd, 0);
65 assert (page != (caddr_t) -1);
66 if (page != alloc_end)
67 alloc_ptr = page;
68 alloc_end = page + pagesize;
71 alloc_last_block = (void *) alloc_ptr;
72 alloc_ptr += n;
73 return alloc_last_block;
75 weak_symbol (malloc)
77 /* This will rarely be called. */
78 void
79 free (void *ptr)
81 /* We can free only the last block allocated. */
82 if (ptr == alloc_last_block)
83 alloc_ptr = alloc_last_block;
85 weak_symbol (free)
87 /* This is never called. */
88 void *
89 realloc (void *ptr, size_t n)
90 { ptr += n; abort (); }
91 weak_symbol (realloc)
93 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
95 #include <setjmp.h>
97 int __sigjmp_save (sigjmp_buf env, int savemask)
98 { env[0].__mask_was_saved = savemask; return 0; }
99 weak_symbol (__sigjmp_save)
101 void
102 longjmp (jmp_buf env, int val) { __longjmp (env[0].__jmpbuf, val); }
103 weak_symbol (longjmp)
106 /* Define our own stub for the localization function used by strerror.
107 English-only in the dynamic linker keeps it smaller. */
109 char *
110 __dgettext (const char *domainname, const char *msgid)
112 assert (domainname == _libc_intl_domainname);
113 return (char *) msgid;
115 weak_symbol (__dgettext)
116 weak_alias (__dgettext, dgettext)
118 #ifndef NDEBUG
120 /* Define (weakly) our own assert failure function which doesn't use stdio.
121 If we are linked into the user program (-ldl), the normal __assert_fail
122 defn can override this one. */
124 void
125 __assert_fail (const char *assertion,
126 const char *file, unsigned int line, const char *function)
128 char buf[64];
129 buf[sizeof buf - 1] = '\0';
130 _dl_sysdep_fatal ("BUG IN DYNAMIC LINKER ld.so: ",
131 file, ": ", _itoa (line, buf + sizeof buf - 1, 10, 0),
132 ": ", function ?: "", function ? ": " : "",
133 "Assertion `", assertion, "' failed!\n",
134 NULL);
137 weak_symbol (__assert_fail)
139 void
140 __assert_perror_fail (int errnum,
141 const char *file, unsigned int line,
142 const char *function)
144 char buf[64];
145 buf[sizeof buf - 1] = '\0';
146 _dl_sysdep_fatal ("BUG IN DYNAMIC LINKER ld.so: ",
147 file, ": ", _itoa (line, buf + sizeof buf - 1, 10, 0),
148 ": ", function ?: "", function ? ": " : "",
149 "Unexpected error: ", strerror (errnum), "\n", NULL);
152 weak_symbol (__assert_perror_fail)
154 #endif