Update.
[platform/upstream/glibc.git] / elf / dl-minimal.c
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.
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 <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"
27
28 /* Minimal `malloc' allocator for use while loading shared libraries.
29    Only small blocks are allocated, and none are ever freed.  */
30
31 static void *alloc_ptr, *alloc_end, *alloc_last_block;
32
33 void * weak_function
34 malloc (size_t n)
35 {
36 #ifdef MAP_ANON
37 #define _dl_zerofd (-1)
38 #else
39   extern int _dl_zerofd;
40
41   if (_dl_zerofd == -1)
42     _dl_zerofd = _dl_sysdep_open_zero_fill ();
43 #define MAP_ANON 0
44 #endif
45
46   if (_dl_pagesize == 0)
47     _dl_pagesize = __getpagesize ();
48
49   if (alloc_end == 0)
50     {
51       /* Consume any unused space in the last page of our data segment.  */
52       extern int _end;
53       alloc_ptr = &_end;
54       alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0) + _dl_pagesize - 1)
55                                 & ~(_dl_pagesize - 1));
56     }
57
58   /* Make sure the allocation pointer is ideally aligned.  */
59   alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + sizeof (double) - 1)
60                             & ~(sizeof (double) - 1));
61
62   if (alloc_ptr + n >= alloc_end)
63     {
64       /* Insufficient space left; allocate another page.  */
65       caddr_t page;
66       assert (n <= _dl_pagesize);
67       page = __mmap (0, _dl_pagesize, PROT_READ|PROT_WRITE,
68                      MAP_ANON|MAP_PRIVATE, _dl_zerofd, 0);
69       assert (page != (caddr_t) -1);
70       if (page != alloc_end)
71         alloc_ptr = page;
72       alloc_end = page + _dl_pagesize;
73     }
74
75   alloc_last_block = (void *) alloc_ptr;
76   alloc_ptr += n;
77   return alloc_last_block;
78 }
79
80 /* We use this function occasionally since the real implementation may
81    be optimized when it can assume the memory it returns already is
82    set to NUL.  */
83 void * weak_function
84 calloc (size_t nmemb, size_t size)
85 {
86   size_t total = nmemb * size;
87   void *result = malloc (total);
88   return memset (result, '\0', total);
89 }
90
91 /* This will rarely be called.  */
92 void weak_function
93 free (void *ptr)
94 {
95   /* We can free only the last block allocated.  */
96   if (ptr == alloc_last_block)
97     alloc_ptr = alloc_last_block;
98 }
99
100 /* This is only called with the most recent block returned by malloc.  */
101 void * weak_function
102 realloc (void *ptr, size_t n)
103 {
104   void *new;
105   assert (ptr == alloc_last_block);
106   alloc_ptr = alloc_last_block;
107   new = malloc (n);
108   assert (new == ptr);
109   return new;
110 }
111 \f
112 /* Avoid signal frobnication in setjmp/longjmp.  Keeps things smaller.  */
113
114 #include <setjmp.h>
115
116 int weak_function
117 __sigjmp_save (sigjmp_buf env, int savemask)
118 { env[0].__mask_was_saved = savemask; return 0; }
119
120 void weak_function
121 longjmp (jmp_buf env, int val) { __longjmp (env[0].__jmpbuf, val); }
122 \f
123 /* Define our own stub for the localization function used by strerror.
124    English-only in the dynamic linker keeps it smaller.  */
125
126 char * weak_function
127 __dcgettext (const char *domainname, const char *msgid, int category)
128 {
129   return (char *) msgid;
130 }
131 weak_alias (__dcgettext, dcgettext)
132 \f
133 #ifndef NDEBUG
134
135 /* Define (weakly) our own assert failure function which doesn't use stdio.
136    If we are linked into the user program (-ldl), the normal __assert_fail
137    defn can override this one.  */
138
139 void weak_function
140 __assert_fail (const char *assertion,
141                const char *file, unsigned int line, const char *function)
142 {
143   char buf[64];
144   buf[sizeof buf - 1] = '\0';
145   _dl_sysdep_fatal ("BUG IN DYNAMIC LINKER ld.so: ",
146                     file, ": ", _itoa (line, buf + sizeof buf - 1, 10, 0),
147                     ": ", function ?: "", function ? ": " : "",
148                     "Assertion `", assertion, "' failed!\n",
149                     NULL);
150
151 }
152
153 void weak_function
154 __assert_perror_fail (int errnum,
155                       const char *file, unsigned int line,
156                       const char *function)
157 {
158   char buf[64];
159   buf[sizeof buf - 1] = '\0';
160   _dl_sysdep_fatal ("BUG IN DYNAMIC LINKER ld.so: ",
161                     file, ": ", _itoa (line, buf + sizeof buf - 1, 10, 0),
162                     ": ", function ?: "", function ? ": " : "",
163                     "Unexpected error: ", strerror (errnum), "\n", NULL);
164
165 }
166
167 #endif