S390: Get rid of linknamespace failures for string functions.
[platform/upstream/glibc.git] / elf / dl-map-segments.h
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.
4
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.
9
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.
14
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/>.  */
18
19 #include <dl-load.h>
20
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).  */
27
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)
34 {
35   const struct loadcmd *c = loadcmds;
36
37   if (__glibc_likely (type == ET_DYN))
38     {
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.
46
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. */
50       ElfW(Addr) mappref
51         = (ELF_PREFERRED_ADDRESS (loader, maplength,
52                                   c->mapstart & GLRO(dl_use_load_bias))
53            - MAP_BASE_ADDR (l));
54
55       /* Remember which part of the address space this object uses.  */
56       l->l_map_start = (ElfW(Addr)) __mmap ((void *) mappref, maplength,
57                                             c->prot,
58                                             MAP_COPY|MAP_FILE,
59                                             fd, c->mapoff);
60       if (__glibc_unlikely ((void *) l->l_map_start == MAP_FAILED))
61         return DL_MAP_SEGMENTS_ERROR_MAP_SEGMENT;
62
63       l->l_map_end = l->l_map_start + maplength;
64       l->l_addr = l->l_map_start - c->mapstart;
65
66       if (has_holes)
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
71            mapping.  */
72         __mprotect ((caddr_t) (l->l_addr + c->mapend),
73                     loadcmds[nloadcmds - 1].mapstart - c->mapend,
74                     PROT_NONE);
75
76       l->l_contiguous = 1;
77
78       goto postmap;
79     }
80
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;
85
86   while (c < &loadcmds[nloadcmds])
87     {
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,
93                       fd, c->mapoff)
94               == MAP_FAILED))
95         return DL_MAP_SEGMENTS_ERROR_MAP_SEGMENT;
96
97     postmap:
98       _dl_postprocess_loadcmd (l, header, c);
99
100       if (c->allocend > c->dataend)
101         {
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;
105
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));
110
111           if (zeroend < zeropage)
112             /* All the extra data is in the last page of the segment.
113                We can just zero it.  */
114             zeropage = zeroend;
115
116           if (zeropage > zero)
117             {
118               /* Zero the final part of the last page of the segment.  */
119               if (__glibc_unlikely ((c->prot & PROT_WRITE) == 0))
120                 {
121                   /* Dag nab it.  */
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;
126                 }
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);
131             }
132
133           if (zeroend > zeropage)
134             {
135               /* Map the remaining zero pages in from the zero fill FD.  */
136               caddr_t mapat;
137               mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
138                               c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
139                               -1, 0);
140               if (__glibc_unlikely (mapat == MAP_FAILED))
141                 return DL_MAP_SEGMENTS_ERROR_MAP_ZERO_FILL;
142             }
143         }
144
145       ++c;
146     }
147
148   /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
149      fixed.  */
150   ELF_FIXED_ADDRESS (loader, c->mapstart);
151
152   return NULL;
153 }