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