1 /* Map in a shared object's segments. Generic version.
2 Copyright (C) 1995-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
21 /* This implementation assumes (as does the corresponding implementation
22 of _dl_unmap_segments, in dl-unmap-segments.h) that shared objects
23 are always laid out with all segments contiguous (or with gaps
24 between them small enough that it's preferable to reserve all whole
25 pages inside the gaps with PROT_NONE mappings rather than permitting
26 other use of those parts of the address space). */
28 static __always_inline const char *
29 _dl_map_segments (struct link_map *l, int fd,
30 const ElfW(Ehdr) *header, int type,
31 const struct loadcmd loadcmds[], size_t nloadcmds,
32 const size_t maplength, bool has_holes,
33 struct link_map *loader)
35 const struct loadcmd *c = loadcmds;
37 if (__glibc_likely (type == ET_DYN))
39 /* This is a position-independent shared object. We can let the
40 kernel map it anywhere it likes, but we must have space for all
41 the segments in their specified positions relative to the first.
42 So we map the first segment without MAP_FIXED, but with its
43 extent increased to cover all the segments. Then we remove
44 access from excess portion, and there is known sufficient space
45 there to remap from the later segments.
47 As a refinement, sometimes we have an address that we would
48 prefer to map such objects at; but this is only a preference,
49 the OS can do whatever it likes. */
51 = (ELF_PREFERRED_ADDRESS (loader, maplength,
52 c->mapstart & GLRO(dl_use_load_bias))
55 /* Remember which part of the address space this object uses. */
56 l->l_map_start = (ElfW(Addr)) __mmap ((void *) mappref, maplength,
60 if (__glibc_unlikely ((void *) l->l_map_start == MAP_FAILED))
61 return DL_MAP_SEGMENTS_ERROR_MAP_SEGMENT;
63 l->l_map_end = l->l_map_start + maplength;
64 l->l_addr = l->l_map_start - c->mapstart;
67 /* Change protection on the excess portion to disallow all access;
68 the portions we do not remap later will be inaccessible as if
69 unallocated. Then jump into the normal segment-mapping loop to
70 handle the portion of the segment past the end of the file
72 __mprotect ((caddr_t) (l->l_addr + c->mapend),
73 loadcmds[nloadcmds - 1].mapstart - c->mapend,
81 /* Remember which part of the address space this object uses. */
82 l->l_map_start = c->mapstart + l->l_addr;
83 l->l_map_end = l->l_map_start + maplength;
84 l->l_contiguous = !has_holes;
86 while (c < &loadcmds[nloadcmds])
88 if (c->mapend > c->mapstart
89 /* Map the segment contents from the file. */
90 && (__mmap ((void *) (l->l_addr + c->mapstart),
91 c->mapend - c->mapstart, c->prot,
92 MAP_FIXED|MAP_COPY|MAP_FILE,
95 return DL_MAP_SEGMENTS_ERROR_MAP_SEGMENT;
98 _dl_postprocess_loadcmd (l, header, c);
100 if (c->allocend > c->dataend)
102 /* Extra zero pages should appear at the end of this segment,
103 after the data mapped from the file. */
104 ElfW(Addr) zero, zeroend, zeropage;
106 zero = l->l_addr + c->dataend;
107 zeroend = l->l_addr + c->allocend;
108 zeropage = ((zero + GLRO(dl_pagesize) - 1)
109 & ~(GLRO(dl_pagesize) - 1));
111 if (zeroend < zeropage)
112 /* All the extra data is in the last page of the segment.
113 We can just zero it. */
118 /* Zero the final part of the last page of the segment. */
119 if (__glibc_unlikely ((c->prot & PROT_WRITE) == 0))
122 if (__mprotect ((caddr_t) (zero
123 & ~(GLRO(dl_pagesize) - 1)),
124 GLRO(dl_pagesize), c->prot|PROT_WRITE) < 0)
125 return DL_MAP_SEGMENTS_ERROR_MPROTECT;
127 memset ((void *) zero, '\0', zeropage - zero);
128 if (__glibc_unlikely ((c->prot & PROT_WRITE) == 0))
129 __mprotect ((caddr_t) (zero & ~(GLRO(dl_pagesize) - 1)),
130 GLRO(dl_pagesize), c->prot);
133 if (zeroend > zeropage)
135 /* Map the remaining zero pages in from the zero fill FD. */
137 mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
138 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
140 if (__glibc_unlikely (mapat == MAP_FAILED))
141 return DL_MAP_SEGMENTS_ERROR_MAP_ZERO_FILL;
148 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
150 ELF_FIXED_ADDRESS (loader, c->mapstart);