1/* mremap.c -- version of mremap for gold.  */
2
3/* Copyright (C) 2009-2017 Free Software Foundation, Inc.
4   Written by Ian Lance Taylor <iant@google.com>.
5
6   This file is part of gold.
7
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.
12
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.
17
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.  */
22
23#include "config.h"
24#include "ansidecl.h"
25
26#include <errno.h>
27#include <string.h>
28#include <sys/types.h>
29
30#ifdef HAVE_SYS_MMAN_H
31#include <sys/mman.h>
32#endif
33
34extern void *gold_mremap (void *, size_t, size_t, int);
35
36#ifdef HAVE_MMAP
37
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.  */
44
45/* Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS.  */
46
47#ifndef MAP_ANONYMOUS
48# define MAP_ANONYMOUS MAP_ANON
49#endif
50
51void *
52gold_mremap (void *old_address, size_t old_size, size_t new_size,
53	     int flags ATTRIBUTE_UNUSED)
54{
55  void *ret;
56
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;
65}
66
67#else /* !defined(HAVE_MMAP) */
68
69#ifndef MAP_FAILED
70#define MAP_FAILED ((void *) -1)
71#endif
72
73#ifndef ENOSYS
74#define ENOSYS EINVAL
75#endif
76
77void *
78gold_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)
82{
83  errno = ENOSYS;
84  return MAP_FAILED;
85}
86
87#endif /* !defined(HAVE_MMAP) */
88