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