aboutsummaryrefslogtreecommitdiff
path: root/sysdeps/x86_64/multiarch/memmove-c.c
blob: 7111128e7517f7cffec9fb3ad923f4fd9d92f0da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* C-version of memmove for using when Intel MPX is enabled
   in order to prosess with a buffer of pointers correctly.
   Copyright (C) 2013 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   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,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <stddef.h>

void *
__memmove (void *dst, const void *src, size_t n)
{
  if (n == 0) return dst;

  __bnd_chk_ptr_lbounds(dst);
  __bnd_chk_ptr_ubounds(dst+n-1);
#ifndef __CHKWR__
  __bnd_chk_ptr_lbounds(src);
  __bnd_chk_ptr_ubounds(src+n-1);
#endif
  return chkp_memmove_nochk(dst, src, n);
}


void *
chkp_memmove_nochk (void *dst, const void *src, size_t n)
{
  const char *s = src;
  char *d = dst;
  void *ret = dst;
  size_t offset_src = ((size_t) s) & (sizeof(size_t) - 1);
  size_t offset_dst = ((size_t) d) & (sizeof(size_t) - 1);

  if (offset_src != offset_dst)
  {
    if (s < d)
    {
      /* backward copying */
      d += n;
      s += n;
      while (n--)
        *--d = *--s;
    }
    else
      /* forward copying */
      while (n--)
        *d++ = *s++;
  }
  else
  {
    if (s < d)
    {
      offset_src = (offset_src + (size_t)src) & (sizeof(size_t) - 1);
      /* backward copying */
      d += n;
      s += n;
      while (n-- && offset_src--)
        *--d = *--s;
      n++;
      if (!n) return ret;
      void **d1 = (void **)d;
      void **s1 = (void **)s;
      while (n >= sizeof(void *))
      {
        n -= sizeof(void *);
        *--d1 = *--s1;
      }
      s = (char *)s1;
      d = (char *)d1;
      while (n--)
        *--d = *--s;
    }
    else
    {
      if (offset_src) offset_src = sizeof(size_t) - offset_src;
      /* forward copying */
      while (n-- && offset_src--)
        *d++ = *s++;
      n++;
      if (!n) return ret;
      void **d1 = (void **)d;
      void **s1 = (void **)s;
      while (n >= sizeof(void *))
      {
        n -= sizeof(void *);
        *d1++ = *s1++;
      }
      s = (char *)s1;
      d = (char *)d1;
      while (n--)
        *d++ = *s++;
    }
  }
  return ret;
}

weak_alias (__memmove, __libc_memmove)
weak_alias (__memmove, __GI_memmove)
weak_alias (__memmove, memmove)

# if defined SHARED && !defined NOT_IN_libc
#  include <shlib-compat.h>
#  if SHLIB_COMPAT (libc, GLIBC_2_2_5, GLIBC_2_14)
compat_symbol (libc, memmove, memcpy, GLIBC_2_2_5);
#  endif
# endif