1 /* Copyright (C) 2003, 2004 Red Hat, Inc.
2 Contributed by Alexandre Oliva <aoliva@redhat.com>
4 This file is part of uClibc.
6 uClibc is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 uClibc is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with uClibc; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
21 /* We can't use the real errno in ldso, since it has not yet
22 * been dynamicly linked in yet. */
23 #include "sys/syscall.h"
26 #define __set_errno(X) {(_dl_errno) = (X);}
29 /* The code below is extracted from libc/sysdeps/linux/frv/_mmap.c */
31 #if DYNAMIC_LOADER_IN_SIMULATOR
32 #define __NR___syscall_mmap2 __NR_mmap2
33 static inline _syscall6(__ptr_t, __syscall_mmap2, __ptr_t, addr,
34 size_t, len, int, prot, int, flags, int, fd, off_t, offset);
36 /* Make sure we don't get another definition of _dl_mmap from the
37 machine-independent code. */
41 /* This is always 12, even on architectures where PAGE_SHIFT != 12. */
42 # ifndef MMAP2_PAGE_SHIFT
43 # define MMAP2_PAGE_SHIFT 12
46 #include <bits/uClibc_page.h> /* for PAGE_SIZE */
47 inline static void *_dl_memset(void*,int,size_t);
48 inline static ssize_t _dl_pread(int fd, void *buf, size_t count, off_t offset);
51 _dl_mmap(__ptr_t addr, size_t len, int prot, int flags, int fd, __off_t offset)
53 size_t plen = (len + PAGE_SIZE - 1) & -PAGE_SIZE;
55 /* This is a hack to enable the dynamic loader to run within a
56 simulator that doesn't support mmap, with a number of very ugly
57 tricks. Also, it's not as useful as it sounds, since only dynamic
58 executables without DT_NEEDED dependencies can be run. AFAIK, they
59 can only be created with -pie. This trick suffices to enable the
60 dynamic loader to obtain a blank page that it maps early in the
62 if ((flags & MAP_FIXED) == 0)
64 void *_dl_mmap_base = 0;
70 asm ("mov sp, %0" : "=r" (stack));
71 _dl_mmap_base = (void *)(((long)stack + 2 * PAGE_SIZE) & -PAGE_SIZE);
73 if (((void **)_dl_mmap_base)[0] == _dl_mmap_base
74 && ((void **)_dl_mmap_base)[1023] == _dl_mmap_base
75 && (((void **)_dl_mmap_base)[177]
76 == ((void **)_dl_mmap_base)[771]))
78 while (((void**)_dl_mmap_base)[177])
80 _dl_mmap_base = ((void**)_dl_mmap_base)[177];
81 if (!(((void **)_dl_mmap_base)[0] == _dl_mmap_base
82 && ((void **)_dl_mmap_base)[1023] == _dl_mmap_base
83 && (((void **)_dl_mmap_base)[177]
84 == ((void**)_dl_mmap_base)[771])))
91 for (i = 0; i < (int)PAGE_SIZE; i++)
92 if (*(char*)(_dl_mmap_base + i))
96 _dl_mmap_base = (void*)((long)_dl_mmap_base + PAGE_SIZE);
99 ((void**)_dl_mmap_base)[-1] =
100 ((void**)_dl_mmap_base)[0] =
101 ((void**)_dl_mmap_base)[1023] =
108 if (!(((void **)_dl_mmap_base)[0] == _dl_mmap_base
109 && ((void **)_dl_mmap_base)[1023] == _dl_mmap_base
110 && (((void **)_dl_mmap_base)[177]
111 == ((void**)_dl_mmap_base)[771])))
113 ret = (__ptr_t)((char*)_dl_mmap_base + PAGE_SIZE);
115 ((void**)_dl_mmap_base)[177] =
116 ((void**)_dl_mmap_base)[771] =
117 (char*)_dl_mmap_base + plen + PAGE_SIZE;
118 ((void**)_dl_mmap_base)[0] =
119 ((void**)_dl_mmap_base)[1023] =
123 if ((flags & MAP_ANONYMOUS) != 0)
125 _dl_memset (ret, 0, plen);
132 if (offset & ((1 << MMAP2_PAGE_SHIFT) - 1)) {
134 __set_errno (EINVAL);
138 if ((flags & MAP_FIXED) != 0)
140 if (_dl_pread(fd, addr, len, offset) != (ssize_t)len)
141 return (void*)MAP_FAILED;
143 _dl_memset (addr + len, 0, plen - len);
146 return(__syscall_mmap2(addr, len, prot, flags, fd, (off_t) (offset >> MMAP2_PAGE_SHIFT)));
151 #ifdef DYNAMIC_LOADER_IN_SIMULATOR
154 #define __NR___syscall_lseek __NR_lseek
155 inline static unsigned long _dl_read(int fd, const void *buf, unsigned long count);
157 inline static _syscall3(__off_t, __syscall_lseek, int, fd, __off_t, offset,
159 inline static ssize_t
160 _dl_pread(int fd, void *buf, size_t count, off_t offset)
162 __off_t orig = __syscall_lseek (fd, 0, SEEK_CUR);
168 if (__syscall_lseek (fd, offset, SEEK_SET) != offset)
171 ret = _dl_read (fd, buf, count);
173 if (__syscall_lseek (fd, orig, SEEK_SET) != orig)
179 #define __NR___syscall_pread __NR_pread
180 inline static _syscall5(ssize_t, __syscall_pread, int, fd, void *, buf,
181 size_t, count, off_t, offset_hi, off_t, offset_lo);
183 inline static ssize_t
184 _dl_pread(int fd, void *buf, size_t count, off_t offset)
186 return(__syscall_pread(fd,buf,count,__LONG_LONG_PAIR (offset >> 31, offset)));
191 #ifdef __NR_sram_alloc
192 #define __NR__dl_sram_alloc __NR_sram_alloc
193 inline static _syscall2(__ptr_t, _dl_sram_alloc,
194 size_t, len, unsigned long, flags);
197 #ifdef __NR_sram_free
198 #define __NR__dl_sram_free __NR_sram_free
199 inline static _syscall1(int, _dl_sram_free, __ptr_t, addr);
202 #ifdef __NR_dma_memcpy
203 #define __NR__dl_dma_memcpy __NR_dma_memcpy
204 inline static _syscall3(__ptr_t, _dl_dma_memcpy,
205 __ptr_t, dest, __ptr_t, src, size_t, len);
208 #define __UCLIBC_MMAP_HAS_6_ARGS__