OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc1/] [newlib/] [libc/] [sys/] [linux/] [dl/] [dl-minimal.c] - Diff between revs 207 and 345

Only display areas with differences | Details | Blame | View Log

Rev 207 Rev 345
/* Minimal replacements for basic facilities used in the dynamic linker.
/* Minimal replacements for basic facilities used in the dynamic linker.
   Copyright (C) 1995,96,97,98,2000,2001 Free Software Foundation, Inc.
   Copyright (C) 1995,96,97,98,2000,2001 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   This file is part of the GNU C Library.
 
 
   The GNU C Library is free software; you can redistribute it and/or
   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.
   version 2.1 of the License, or (at your option) any later version.
 
 
   The GNU C Library is distributed in the hope that it will be useful,
   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
   Lesser General Public License for more details.
 
 
   You should have received a copy of the GNU Lesser General Public
   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */
   02111-1307 USA.  */
 
 
#include <errno.h>
#include <errno.h>
#include <limits.h>
#include <limits.h>
#include <string.h>
#include <string.h>
#include <unistd.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/mman.h>
#include <ldsodefs.h>
#include <ldsodefs.h>
#include <machine/weakalias.h>
#include <machine/weakalias.h>
 
 
#include <assert.h>
#include <assert.h>
 
 
/* Minimal `malloc' allocator for use while loading shared libraries.
/* Minimal `malloc' allocator for use while loading shared libraries.
   No block is ever freed.  */
   No block is ever freed.  */
 
 
static void *alloc_ptr, *alloc_end, *alloc_last_block;
static void *alloc_ptr, *alloc_end, *alloc_last_block;
 
 
/* Declarations of global functions.  */
/* Declarations of global functions.  */
extern void weak_function free (void *ptr);
extern void weak_function free (void *ptr);
extern void * weak_function realloc (void *ptr, size_t n);
extern void * weak_function realloc (void *ptr, size_t n);
extern unsigned long int weak_function __strtoul_internal
extern unsigned long int weak_function __strtoul_internal
(const char *nptr, char **endptr, int base, int group);
(const char *nptr, char **endptr, int base, int group);
extern unsigned long int weak_function strtoul (const char *nptr,
extern unsigned long int weak_function strtoul (const char *nptr,
                                                char **endptr, int base);
                                                char **endptr, int base);
 
 
 
 
void * weak_function
void * weak_function
malloc (size_t n)
malloc (size_t n)
{
{
#ifdef MAP_ANON
#ifdef MAP_ANON
#define _dl_zerofd (-1)
#define _dl_zerofd (-1)
#else
#else
  extern int _dl_zerofd;
  extern int _dl_zerofd;
 
 
  if (_dl_zerofd == -1)
  if (_dl_zerofd == -1)
    _dl_zerofd = _dl_sysdep_open_zero_fill ();
    _dl_zerofd = _dl_sysdep_open_zero_fill ();
#define MAP_ANON 0
#define MAP_ANON 0
#endif
#endif
 
 
  if (alloc_end == 0)
  if (alloc_end == 0)
    {
    {
      /* Consume any unused space in the last page of our data segment.  */
      /* Consume any unused space in the last page of our data segment.  */
      extern int _end;
      extern int _end;
      alloc_ptr = &_end;
      alloc_ptr = &_end;
      alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0) + _dl_pagesize - 1)
      alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0) + _dl_pagesize - 1)
                                & ~(_dl_pagesize - 1));
                                & ~(_dl_pagesize - 1));
    }
    }
 
 
  /* Make sure the allocation pointer is ideally aligned.  */
  /* Make sure the allocation pointer is ideally aligned.  */
  alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + sizeof (double) - 1)
  alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + sizeof (double) - 1)
                            & ~(sizeof (double) - 1));
                            & ~(sizeof (double) - 1));
 
 
  if (alloc_ptr + n >= alloc_end)
  if (alloc_ptr + n >= alloc_end)
    {
    {
      /* Insufficient space left; allocate another page.  */
      /* Insufficient space left; allocate another page.  */
      caddr_t page;
      caddr_t page;
      size_t nup = (n + _dl_pagesize - 1) & ~(_dl_pagesize - 1);
      size_t nup = (n + _dl_pagesize - 1) & ~(_dl_pagesize - 1);
      page = __mmap (0, nup, PROT_READ|PROT_WRITE,
      page = __mmap (0, nup, PROT_READ|PROT_WRITE,
                     MAP_ANON|MAP_PRIVATE, _dl_zerofd, 0);
                     MAP_ANON|MAP_PRIVATE, _dl_zerofd, 0);
      assert (page != MAP_FAILED);
      assert (page != MAP_FAILED);
      if (page != alloc_end)
      if (page != alloc_end)
        alloc_ptr = page;
        alloc_ptr = page;
      alloc_end = page + nup;
      alloc_end = page + nup;
    }
    }
 
 
  alloc_last_block = (void *) alloc_ptr;
  alloc_last_block = (void *) alloc_ptr;
  alloc_ptr += n;
  alloc_ptr += n;
  return alloc_last_block;
  return alloc_last_block;
}
}
 
 
/* We use this function occasionally since the real implementation may
/* We use this function occasionally since the real implementation may
   be optimized when it can assume the memory it returns already is
   be optimized when it can assume the memory it returns already is
   set to NUL.  */
   set to NUL.  */
void * weak_function
void * weak_function
calloc (size_t nmemb, size_t size)
calloc (size_t nmemb, size_t size)
{
{
  size_t total = nmemb * size;
  size_t total = nmemb * size;
  void *result = malloc (total);
  void *result = malloc (total);
  return memset (result, '\0', total);
  return memset (result, '\0', total);
}
}
 
 
/* This will rarely be called.  */
/* This will rarely be called.  */
void weak_function
void weak_function
free (void *ptr)
free (void *ptr)
{
{
  /* We can free only the last block allocated.  */
  /* We can free only the last block allocated.  */
  if (ptr == alloc_last_block)
  if (ptr == alloc_last_block)
    alloc_ptr = alloc_last_block;
    alloc_ptr = alloc_last_block;
}
}
 
 
/* This is only called with the most recent block returned by malloc.  */
/* This is only called with the most recent block returned by malloc.  */
void * weak_function
void * weak_function
realloc (void *ptr, size_t n)
realloc (void *ptr, size_t n)
{
{
  void *new;
  void *new;
  if (ptr == NULL)
  if (ptr == NULL)
    return malloc (n);
    return malloc (n);
  assert (ptr == alloc_last_block);
  assert (ptr == alloc_last_block);
  alloc_ptr = alloc_last_block;
  alloc_ptr = alloc_last_block;
  new = malloc (n);
  new = malloc (n);
  assert (new == ptr);
  assert (new == ptr);
  return new;
  return new;
}
}
 
 


/* Define our own version of the internal function used by strerror.  We
/* Define our own version of the internal function used by strerror.  We
   only provide the messages for some common errors.  This avoids pulling
   only provide the messages for some common errors.  This avoids pulling
   in the whole error list.  */
   in the whole error list.  */
 
 
char * weak_function
char * weak_function
__strerror_r (int errnum, char *buf, size_t buflen)
__strerror_r (int errnum, char *buf, size_t buflen)
{
{
  char *msg;
  char *msg;
 
 
  switch (errnum)
  switch (errnum)
    {
    {
    case ENOMEM:
    case ENOMEM:
      msg = (char *) "Cannot allocate memory";
      msg = (char *) "Cannot allocate memory";
      break;
      break;
    case EINVAL:
    case EINVAL:
      msg = (char *) "Invalid argument";
      msg = (char *) "Invalid argument";
      break;
      break;
    case ENOENT:
    case ENOENT:
      msg = (char *) "No such file or directory";
      msg = (char *) "No such file or directory";
      break;
      break;
    case EPERM:
    case EPERM:
      msg = (char *) "Operation not permitted";
      msg = (char *) "Operation not permitted";
      break;
      break;
    case EIO:
    case EIO:
      msg = (char *) "Input/output error";
      msg = (char *) "Input/output error";
      break;
      break;
    case EACCES:
    case EACCES:
      msg = (char *) "Permission denied";
      msg = (char *) "Permission denied";
      break;
      break;
    default:
    default:
      /* No need to check buffer size, all calls in the dynamic linker
      /* No need to check buffer size, all calls in the dynamic linker
         provide enough space.  */
         provide enough space.  */
      msg = (char *) "Error";
      msg = (char *) "Error";
      break;
      break;
    }
    }
 
 
  return msg;
  return msg;
}
}


#ifndef NDEBUG
#ifndef NDEBUG
 
 
/* Define (weakly) our own assert failure function which doesn't use stdio.
/* Define (weakly) our own assert failure function which doesn't use stdio.
   If we are linked into the user program (-ldl), the normal __assert_fail
   If we are linked into the user program (-ldl), the normal __assert_fail
   defn can override this one.  */
   defn can override this one.  */
 
 
void weak_function
void weak_function
__assert_fail (const char *assertion,
__assert_fail (const char *assertion,
               const char *file, unsigned int line, const char *function)
               const char *file, unsigned int line, const char *function)
{
{
  _dl_fatal_printf ("\
  _dl_fatal_printf ("\
Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
                    file, line, function ?: "", function ? ": " : "",
                    file, line, function ?: "", function ? ": " : "",
                    assertion);
                    assertion);
 
 
}
}
 
 
void weak_function
void weak_function
__assert_perror_fail (int errnum,
__assert_perror_fail (int errnum,
                      const char *file, unsigned int line,
                      const char *file, unsigned int line,
                      const char *function)
                      const char *function)
{
{
  char errbuf[64];
  char errbuf[64];
  _dl_fatal_printf ("\
  _dl_fatal_printf ("\
Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s\n",
Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s\n",
                    file, line, function ?: "", function ? ": " : "",
                    file, line, function ?: "", function ? ": " : "",
                    __strerror_r (errnum, errbuf, sizeof (errbuf)));
                    __strerror_r (errnum, errbuf, sizeof (errbuf)));
}
}
 
 
#endif
#endif
 
 
unsigned long int weak_function
unsigned long int weak_function
__strtoul_internal (const char *nptr, char **endptr, int base, int group)
__strtoul_internal (const char *nptr, char **endptr, int base, int group)
{
{
  unsigned long int result = 0;
  unsigned long int result = 0;
  long int sign = 1;
  long int sign = 1;
 
 
  while (*nptr == ' ' || *nptr == '\t')
  while (*nptr == ' ' || *nptr == '\t')
    ++nptr;
    ++nptr;
 
 
  if (*nptr == '-')
  if (*nptr == '-')
    {
    {
      sign = -1;
      sign = -1;
      ++nptr;
      ++nptr;
    }
    }
  else if (*nptr == '+')
  else if (*nptr == '+')
    ++nptr;
    ++nptr;
 
 
  if (*nptr < '0' || *nptr > '9')
  if (*nptr < '0' || *nptr > '9')
    {
    {
      if (endptr != NULL)
      if (endptr != NULL)
        *endptr = (char *) nptr;
        *endptr = (char *) nptr;
      return 0UL;
      return 0UL;
    }
    }
 
 
  assert (base == 0);
  assert (base == 0);
  base = 10;
  base = 10;
  if (*nptr == '0')
  if (*nptr == '0')
    {
    {
      if (nptr[1] == 'x' || nptr[1] == 'X')
      if (nptr[1] == 'x' || nptr[1] == 'X')
        {
        {
          base = 16;
          base = 16;
          nptr += 2;
          nptr += 2;
        }
        }
      else
      else
        base = 8;
        base = 8;
    }
    }
 
 
  while (*nptr >= '0' && *nptr <= '9')
  while (*nptr >= '0' && *nptr <= '9')
    {
    {
      unsigned long int digval = *nptr - '0';
      unsigned long int digval = *nptr - '0';
      if (result > LONG_MAX / 10
      if (result > LONG_MAX / 10
          || (result == ULONG_MAX / 10 && digval > ULONG_MAX % 10))
          || (result == ULONG_MAX / 10 && digval > ULONG_MAX % 10))
        {
        {
          errno = ERANGE;
          errno = ERANGE;
          if (endptr != NULL)
          if (endptr != NULL)
            *endptr = (char *) nptr;
            *endptr = (char *) nptr;
          return ULONG_MAX;
          return ULONG_MAX;
        }
        }
      result *= base;
      result *= base;
      result += digval;
      result += digval;
      ++nptr;
      ++nptr;
    }
    }
 
 
  if (endptr != NULL)
  if (endptr != NULL)
    *endptr = (char *) nptr;
    *endptr = (char *) nptr;
  return result * sign;
  return result * sign;
}
}
 
 

powered by: WebSVN 2.1.0

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