OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [newlib-1.18.0/] [newlib/] [libc/] [sys/] [linux/] [dl/] [dl-minimal.c] - Blame information for rev 816

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 207 jeremybenn
/* Minimal replacements for basic facilities used in the dynamic linker.
2
   Copyright (C) 1995,96,97,98,2000,2001 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 Lesser General Public
7
   License as published by the Free Software Foundation; either
8
   version 2.1 of the 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
   Lesser General Public License for more details.
14
 
15
   You should have received a copy of the GNU Lesser General Public
16
   License along with the GNU C Library; if not, write to the Free
17
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18
   02111-1307 USA.  */
19
 
20
#include <errno.h>
21
#include <limits.h>
22
#include <string.h>
23
#include <unistd.h>
24
#include <sys/types.h>
25
#include <sys/mman.h>
26
#include <ldsodefs.h>
27
#include <machine/weakalias.h>
28
 
29
#include <assert.h>
30
 
31
/* Minimal `malloc' allocator for use while loading shared libraries.
32
   No block is ever freed.  */
33
 
34
static void *alloc_ptr, *alloc_end, *alloc_last_block;
35
 
36
/* Declarations of global functions.  */
37
extern void weak_function free (void *ptr);
38
extern void * weak_function realloc (void *ptr, size_t n);
39
extern unsigned long int weak_function __strtoul_internal
40
(const char *nptr, char **endptr, int base, int group);
41
extern unsigned long int weak_function strtoul (const char *nptr,
42
                                                char **endptr, int base);
43
 
44
 
45
void * weak_function
46
malloc (size_t n)
47
{
48
#ifdef MAP_ANON
49
#define _dl_zerofd (-1)
50
#else
51
  extern int _dl_zerofd;
52
 
53
  if (_dl_zerofd == -1)
54
    _dl_zerofd = _dl_sysdep_open_zero_fill ();
55
#define MAP_ANON 0
56
#endif
57
 
58
  if (alloc_end == 0)
59
    {
60
      /* Consume any unused space in the last page of our data segment.  */
61
      extern int _end;
62
      alloc_ptr = &_end;
63
      alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0) + _dl_pagesize - 1)
64
                                & ~(_dl_pagesize - 1));
65
    }
66
 
67
  /* Make sure the allocation pointer is ideally aligned.  */
68
  alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + sizeof (double) - 1)
69
                            & ~(sizeof (double) - 1));
70
 
71
  if (alloc_ptr + n >= alloc_end)
72
    {
73
      /* Insufficient space left; allocate another page.  */
74
      caddr_t page;
75
      size_t nup = (n + _dl_pagesize - 1) & ~(_dl_pagesize - 1);
76
      page = __mmap (0, nup, PROT_READ|PROT_WRITE,
77
                     MAP_ANON|MAP_PRIVATE, _dl_zerofd, 0);
78
      assert (page != MAP_FAILED);
79
      if (page != alloc_end)
80
        alloc_ptr = page;
81
      alloc_end = page + nup;
82
    }
83
 
84
  alloc_last_block = (void *) alloc_ptr;
85
  alloc_ptr += n;
86
  return alloc_last_block;
87
}
88
 
89
/* We use this function occasionally since the real implementation may
90
   be optimized when it can assume the memory it returns already is
91
   set to NUL.  */
92
void * weak_function
93
calloc (size_t nmemb, size_t size)
94
{
95
  size_t total = nmemb * size;
96
  void *result = malloc (total);
97
  return memset (result, '\0', total);
98
}
99
 
100
/* This will rarely be called.  */
101
void weak_function
102
free (void *ptr)
103
{
104
  /* We can free only the last block allocated.  */
105
  if (ptr == alloc_last_block)
106
    alloc_ptr = alloc_last_block;
107
}
108
 
109
/* This is only called with the most recent block returned by malloc.  */
110
void * weak_function
111
realloc (void *ptr, size_t n)
112
{
113
  void *new;
114
  if (ptr == NULL)
115
    return malloc (n);
116
  assert (ptr == alloc_last_block);
117
  alloc_ptr = alloc_last_block;
118
  new = malloc (n);
119
  assert (new == ptr);
120
  return new;
121
}
122
 
123
 
124
/* Define our own version of the internal function used by strerror.  We
125
   only provide the messages for some common errors.  This avoids pulling
126
   in the whole error list.  */
127
 
128
char * weak_function
129
__strerror_r (int errnum, char *buf, size_t buflen)
130
{
131
  char *msg;
132
 
133
  switch (errnum)
134
    {
135
    case ENOMEM:
136
      msg = (char *) "Cannot allocate memory";
137
      break;
138
    case EINVAL:
139
      msg = (char *) "Invalid argument";
140
      break;
141
    case ENOENT:
142
      msg = (char *) "No such file or directory";
143
      break;
144
    case EPERM:
145
      msg = (char *) "Operation not permitted";
146
      break;
147
    case EIO:
148
      msg = (char *) "Input/output error";
149
      break;
150
    case EACCES:
151
      msg = (char *) "Permission denied";
152
      break;
153
    default:
154
      /* No need to check buffer size, all calls in the dynamic linker
155
         provide enough space.  */
156
      msg = (char *) "Error";
157
      break;
158
    }
159
 
160
  return msg;
161
}
162
 
163
#ifndef NDEBUG
164
 
165
/* Define (weakly) our own assert failure function which doesn't use stdio.
166
   If we are linked into the user program (-ldl), the normal __assert_fail
167
   defn can override this one.  */
168
 
169
void weak_function
170
__assert_fail (const char *assertion,
171
               const char *file, unsigned int line, const char *function)
172
{
173
  _dl_fatal_printf ("\
174
Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
175
                    file, line, function ?: "", function ? ": " : "",
176
                    assertion);
177
 
178
}
179
 
180
void weak_function
181
__assert_perror_fail (int errnum,
182
                      const char *file, unsigned int line,
183
                      const char *function)
184
{
185
  char errbuf[64];
186
  _dl_fatal_printf ("\
187
Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s\n",
188
                    file, line, function ?: "", function ? ": " : "",
189
                    __strerror_r (errnum, errbuf, sizeof (errbuf)));
190
}
191
 
192
#endif
193
 
194
unsigned long int weak_function
195
__strtoul_internal (const char *nptr, char **endptr, int base, int group)
196
{
197
  unsigned long int result = 0;
198
  long int sign = 1;
199
 
200
  while (*nptr == ' ' || *nptr == '\t')
201
    ++nptr;
202
 
203
  if (*nptr == '-')
204
    {
205
      sign = -1;
206
      ++nptr;
207
    }
208
  else if (*nptr == '+')
209
    ++nptr;
210
 
211
  if (*nptr < '0' || *nptr > '9')
212
    {
213
      if (endptr != NULL)
214
        *endptr = (char *) nptr;
215
      return 0UL;
216
    }
217
 
218
  assert (base == 0);
219
  base = 10;
220
  if (*nptr == '0')
221
    {
222
      if (nptr[1] == 'x' || nptr[1] == 'X')
223
        {
224
          base = 16;
225
          nptr += 2;
226
        }
227
      else
228
        base = 8;
229
    }
230
 
231
  while (*nptr >= '0' && *nptr <= '9')
232
    {
233
      unsigned long int digval = *nptr - '0';
234
      if (result > LONG_MAX / 10
235
          || (result == ULONG_MAX / 10 && digval > ULONG_MAX % 10))
236
        {
237
          errno = ERANGE;
238
          if (endptr != NULL)
239
            *endptr = (char *) nptr;
240
          return ULONG_MAX;
241
        }
242
      result *= base;
243
      result += digval;
244
      ++nptr;
245
    }
246
 
247
  if (endptr != NULL)
248
    *endptr = (char *) nptr;
249
  return result * sign;
250
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.