mremap.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* mremap.c -- version of mremap for gold. */
  2. /* Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor <iant@google.com>.
  4. This file is part of gold.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "config.h"
  18. #include "ansidecl.h"
  19. #include <errno.h>
  20. #include <string.h>
  21. #include <sys/types.h>
  22. #ifdef HAVE_SYS_MMAN_H
  23. #include <sys/mman.h>
  24. #endif
  25. extern void *gold_mremap (void *, size_t, size_t, int);
  26. #ifdef HAVE_MMAP
  27. /* This file implements mremap for systems which don't have it. The
  28. gold code requires support for mmap. However, there are systems
  29. which have mmap but not mremap. This is not a general replacement
  30. for mremap; it only supports the features which are required for
  31. gold. In particular, we assume that the MREMAP_MAYMOVE flag is
  32. set. */
  33. /* Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS. */
  34. #ifndef MAP_ANONYMOUS
  35. # define MAP_ANONYMOUS MAP_ANON
  36. #endif
  37. void *
  38. gold_mremap (void *old_address, size_t old_size, size_t new_size,
  39. int flags ATTRIBUTE_UNUSED)
  40. {
  41. void *ret;
  42. ret = mmap (0, new_size, PROT_READ | PROT_WRITE,
  43. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  44. if (ret == MAP_FAILED)
  45. return ret;
  46. memcpy (ret, old_address,
  47. old_size < new_size ? old_size : new_size);
  48. (void) munmap (old_address, old_size);
  49. return ret;
  50. }
  51. #else /* !defined(HAVE_MMAP) */
  52. #ifndef MAP_FAILED
  53. #define MAP_FAILED ((void *) -1)
  54. #endif
  55. #ifndef ENOSYS
  56. #define ENOSYS EINVAL
  57. #endif
  58. void *
  59. gold_mremap (void *old_address ATTRIBUTE_UNUSED,
  60. size_t old_size ATTRIBUTE_UNUSED,
  61. size_t new_size ATTRIBUTE_UNUSED,
  62. int flags ATTRIBUTE_UNUSED)
  63. {
  64. errno = ENOSYS;
  65. return MAP_FAILED;
  66. }
  67. #endif /* !defined(HAVE_MMAP) */