1 /* _dl_map_object -- Map in a shared object's segments from the file.
2 Copyright (C) 1995, 1996 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
21 #include <sys/types.h>
28 #include "dynamic-link.h"
31 /* On some systems, no flag bits are given to specify file mapping. */
36 /* The right way to map in the shared library files is MAP_COPY, which
37 makes a virtual copy of the data at the time of the mmap call; this
38 guarantees the mapped pages will be consistent even if the file is
39 overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we
40 get is MAP_PRIVATE, which copies each page when it is modified; this
41 means if the file is overwritten, we may at some point get some pages
42 from the new version after starting with pages from the old version. */
44 #define MAP_COPY MAP_PRIVATE
49 #if BYTE_ORDER == BIG_ENDIAN
50 #define byteorder ELFDATA2MSB
51 #define byteorder_name "big-endian"
52 #elif BYTE_ORDER == LITTLE_ENDIAN
53 #define byteorder ELFDATA2LSB
54 #define byteorder_name "little-endian"
56 #error "Unknown BYTE_ORDER " BYTE_ORDER
57 #define byteorder ELFDATANONE
66 /* Map in the shared object NAME, actually located in REALNAME, and already
70 _dl_map_object_from_fd (const char *name
, int fd
, char *realname
,
71 struct link_map
*loader
, int l_type
)
73 struct link_map
*l
= NULL
;
74 void *file_mapping
= NULL
;
75 size_t mapping_size
= 0;
77 #define LOSE(s) lose (0, (s))
78 void lose (int code
, const char *msg
)
82 __munmap (file_mapping
, mapping_size
);
85 /* Remove the stillborn object from the list and free it. */
87 l
->l_prev
->l_next
= l
->l_next
;
89 l
->l_next
->l_prev
= l
->l_prev
;
93 _dl_signal_error (code
, name
, msg
);
96 inline caddr_t
map_segment (ElfW(Addr
) mapstart
, size_t len
,
97 int prot
, int fixed
, off_t offset
)
99 caddr_t mapat
= __mmap ((caddr_t
) mapstart
, len
, prot
,
100 fixed
|MAP_COPY
|MAP_FILE
,
102 if (mapat
== (caddr_t
) -1)
103 lose (errno
, "failed to map segment from shared object");
107 /* Make sure LOCATION is mapped in. */
108 void *map (off_t location
, size_t size
)
110 if ((off_t
) mapping_size
<= location
+ (off_t
) size
)
114 __munmap (file_mapping
, mapping_size
);
115 mapping_size
= (location
+ size
+ 1 + _dl_pagesize
- 1);
116 mapping_size
&= ~(_dl_pagesize
- 1);
117 result
= __mmap (file_mapping
, mapping_size
, PROT_READ
,
118 MAP_COPY
|MAP_FILE
, fd
, 0);
119 if (result
== (void *) -1)
120 lose (errno
, "cannot map file data");
121 file_mapping
= result
;
123 return file_mapping
+ location
;
126 const ElfW(Ehdr
) *header
;
127 const ElfW(Phdr
) *phdr
;
128 const ElfW(Phdr
) *ph
;
131 /* Look again to see if the real name matched another already loaded. */
132 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
133 if (! strcmp (realname
, l
->l_name
))
135 /* The object is already loaded.
136 Just bump its reference count and return it. */
143 if (_dl_pagesize
== 0)
144 _dl_pagesize
= __getpagesize ();
146 /* Map in the first page to read the header. */
147 header
= map (0, sizeof *header
);
149 /* Check the header for basic validity. */
150 if (*(Elf32_Word
*) &header
->e_ident
!=
151 #if BYTE_ORDER == LITTLE_ENDIAN
152 ((ELFMAG0
<< (EI_MAG0
* 8)) |
153 (ELFMAG1
<< (EI_MAG1
* 8)) |
154 (ELFMAG2
<< (EI_MAG2
* 8)) |
155 (ELFMAG3
<< (EI_MAG3
* 8)))
157 ((ELFMAG0
<< (EI_MAG3
* 8)) |
158 (ELFMAG1
<< (EI_MAG2
* 8)) |
159 (ELFMAG2
<< (EI_MAG1
* 8)) |
160 (ELFMAG3
<< (EI_MAG0
* 8)))
163 LOSE ("invalid ELF header");
164 #define ELF32_CLASS ELFCLASS32
165 #define ELF64_CLASS ELFCLASS64
166 if (header
->e_ident
[EI_CLASS
] != ELFW(CLASS
))
167 LOSE ("ELF file class not " STRING(__ELF_WORDSIZE
) "-bit");
168 if (header
->e_ident
[EI_DATA
] != byteorder
)
169 LOSE ("ELF file data encoding not " byteorder_name
);
170 if (header
->e_ident
[EI_VERSION
] != EV_CURRENT
)
171 LOSE ("ELF file version ident not " STRING(EV_CURRENT
));
172 if (header
->e_version
!= EV_CURRENT
)
173 LOSE ("ELF file version not " STRING(EV_CURRENT
));
174 if (! elf_machine_matches_host (header
->e_machine
))
175 LOSE ("ELF file machine architecture not " ELF_MACHINE_NAME
);
176 if (header
->e_phentsize
!= sizeof (ElfW(Phdr
)))
177 LOSE ("ELF file's phentsize not the expected size");
179 if (_dl_zerofd
== -1)
181 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
182 if (_dl_zerofd
== -1)
185 _dl_signal_error (errno
, NULL
, "cannot open zero fill device");
189 /* Enter the new object in the list of loaded objects. */
190 l
= _dl_new_object (realname
, name
, l_type
);
192 lose (ENOMEM
, "cannot create shared object descriptor");
194 l
->l_loader
= loader
;
196 /* Extract the remaining details we need from the ELF header
197 and then map in the program header table. */
198 l
->l_entry
= header
->e_entry
;
199 type
= header
->e_type
;
200 l
->l_phnum
= header
->e_phnum
;
201 phdr
= map (header
->e_phoff
, l
->l_phnum
* sizeof (ElfW(Phdr
)));
204 /* Scan the program header table, collecting its load commands. */
207 ElfW(Addr
) mapstart
, mapend
, dataend
, allocend
;
210 } loadcmds
[l
->l_phnum
], *c
;
211 size_t nloadcmds
= 0;
216 for (ph
= phdr
; ph
< &phdr
[l
->l_phnum
]; ++ph
)
219 /* These entries tell us where to find things once the file's
220 segments are mapped in. We record the addresses it says
221 verbatim, and later correct for the run-time load address. */
223 l
->l_ld
= (void *) ph
->p_vaddr
;
226 l
->l_phdr
= (void *) ph
->p_vaddr
;
230 /* A load command tells us to map in part of the file.
231 We record the load commands and process them all later. */
232 if (ph
->p_align
% _dl_pagesize
!= 0)
233 LOSE ("ELF load command alignment not page-aligned");
234 if ((ph
->p_vaddr
- ph
->p_offset
) % ph
->p_align
)
235 LOSE ("ELF load command address/offset not properly aligned");
237 struct loadcmd
*c
= &loadcmds
[nloadcmds
++];
238 c
->mapstart
= ph
->p_vaddr
& ~(ph
->p_align
- 1);
239 c
->mapend
= ((ph
->p_vaddr
+ ph
->p_filesz
+ _dl_pagesize
- 1)
240 & ~(_dl_pagesize
- 1));
241 c
->dataend
= ph
->p_vaddr
+ ph
->p_filesz
;
242 c
->allocend
= ph
->p_vaddr
+ ph
->p_memsz
;
243 c
->mapoff
= ph
->p_offset
& ~(ph
->p_align
- 1);
245 if (ph
->p_flags
& PF_R
)
246 c
->prot
|= PROT_READ
;
247 if (ph
->p_flags
& PF_W
)
248 c
->prot
|= PROT_WRITE
;
249 if (ph
->p_flags
& PF_X
)
250 c
->prot
|= PROT_EXEC
;
255 /* We are done reading the file's headers now. Unmap them. */
256 __munmap (file_mapping
, mapping_size
);
258 /* Now process the load commands and map segments into memory. */
261 if (type
== ET_DYN
|| type
== ET_REL
)
263 /* This is a position-independent shared object. We can let the
264 kernel map it anywhere it likes, but we must have space for all
265 the segments in their specified positions relative to the first.
266 So we map the first segment without MAP_FIXED, but with its
267 extent increased to cover all the segments. Then we remove
268 access from excess portion, and there is known sufficient space
269 there to remap from the later segments. */
271 mapat
= map_segment (c
->mapstart
,
272 loadcmds
[nloadcmds
- 1].allocend
- c
->mapstart
,
273 c
->prot
, 0, c
->mapoff
);
274 l
->l_addr
= (ElfW(Addr
)) mapat
- c
->mapstart
;
276 /* Change protection on the excess portion to disallow all access;
277 the portions we do not remap later will be inaccessible as if
278 unallocated. Then jump into the normal segment-mapping loop to
279 handle the portion of the segment past the end of the file
281 __mprotect ((caddr_t
) (l
->l_addr
+ c
->mapend
),
282 loadcmds
[nloadcmds
- 1].allocend
- c
->mapend
,
287 while (c
< &loadcmds
[nloadcmds
])
289 if (c
->mapend
> c
->mapstart
)
290 /* Map the segment contents from the file. */
291 map_segment (l
->l_addr
+ c
->mapstart
, c
->mapend
- c
->mapstart
,
292 c
->prot
, MAP_FIXED
, c
->mapoff
);
295 if (c
->allocend
> c
->dataend
)
297 /* Extra zero pages should appear at the end of this segment,
298 after the data mapped from the file. */
299 ElfW(Addr
) zero
, zeroend
, zeropage
;
301 zero
= l
->l_addr
+ c
->dataend
;
302 zeroend
= l
->l_addr
+ c
->allocend
;
303 zeropage
= (zero
+ _dl_pagesize
- 1) & ~(_dl_pagesize
- 1);
305 if (zeroend
< zeropage
)
306 /* All the extra data is in the last page of the segment.
307 We can just zero it. */
312 /* Zero the final part of the last page of the segment. */
313 if ((c
->prot
& PROT_WRITE
) == 0)
316 if (__mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
317 _dl_pagesize
, c
->prot
|PROT_WRITE
) < 0)
318 lose (errno
, "cannot change memory protections");
320 memset ((void *) zero
, 0, zeropage
- zero
);
321 if ((c
->prot
& PROT_WRITE
) == 0)
322 __mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
323 _dl_pagesize
, c
->prot
);
326 if (zeroend
> zeropage
)
328 /* Map the remaining zero pages in from the zero fill FD. */
330 mapat
= __mmap ((caddr_t
) zeropage
, zeroend
- zeropage
,
331 c
->prot
, MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
,
333 if (mapat
== (caddr_t
) -1)
334 lose (errno
, "cannot map zero-fill pages");
343 /* There was no PT_PHDR specified. We need to find the phdr in the
344 load image ourselves. We assume it is in fact in the load image
345 somewhere, and that the first load command starts at the
346 beginning of the file and thus contains the ELF file header. */
347 ElfW(Addr
) bof
= l
->l_addr
+ loadcmds
[0].mapstart
;
348 assert (loadcmds
[0].mapoff
== 0);
349 l
->l_phdr
= (void *) (bof
+ ((const ElfW(Ehdr
) *) bof
)->e_phoff
);
352 /* Adjust the PT_PHDR value by the runtime load address. */
353 (ElfW(Addr
)) l
->l_phdr
+= l
->l_addr
;
356 /* We are done mapping in the file. We no longer need the descriptor. */
359 if (l
->l_type
== lt_library
&& type
== ET_EXEC
)
360 l
->l_type
= lt_executable
;
365 LOSE ("object file has no dynamic section");
368 (ElfW(Addr
)) l
->l_ld
+= l
->l_addr
;
370 l
->l_entry
+= l
->l_addr
;
372 elf_get_dynamic_info (l
->l_ld
, l
->l_info
);
373 if (l
->l_info
[DT_HASH
])
379 /* Try to open NAME in one of the directories in DIRPATH.
380 Return the fd, or -1. If successful, fill in *REALNAME
381 with the malloc'd full directory name. */
384 open_path (const char *name
, size_t namelen
,
393 if (p
== NULL
|| *p
== '\0')
399 buf
= __alloca (strlen (dirpath
) + 1 + namelen
);
405 p
= strpbrk (dirpath
, ":;");
407 p
= strchr (dirpath
, '\0');
411 /* Two adjacent colons, or a colon at the beginning or the end of
412 the path means to search the current directory. */
413 (void) memcpy (buf
, name
, namelen
);
418 /* Construct the pathname to try. */
419 (void) memcpy (buf
, dirpath
, p
- dirpath
);
420 buf
[p
- dirpath
] = '/';
421 (void) memcpy (&buf
[(p
- dirpath
) + 1], name
, namelen
);
422 buflen
= p
- dirpath
+ 1 + namelen
;
425 fd
= __open (buf
, O_RDONLY
);
428 *realname
= malloc (buflen
);
431 memcpy (*realname
, buf
, buflen
);
436 /* No memory for the name, we certainly won't be able
437 to load and link it. */
442 if (errno
!= ENOENT
&& errno
!= EACCES
)
443 /* The file exists and is readable, but something went wrong. */
446 while (*p
++ != '\0');
451 /* Map in the shared object file NAME. */
454 _dl_map_object (struct link_map
*loader
, const char *name
, int type
)
460 /* Look for this name among those already loaded. */
461 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
462 if (! strcmp (name
, l
->l_libname
) || /* NAME was requested before. */
463 ! strcmp (name
, l
->l_name
) || /* NAME was found before. */
464 /* If the requested name matches the soname of a loaded object,
466 (l
->l_info
[DT_SONAME
] &&
467 ! strcmp (name
, (const char *) (l
->l_addr
+
468 l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
+
469 l
->l_info
[DT_SONAME
]->d_un
.d_val
))))
471 /* The object is already loaded.
472 Just bump its reference count and return it. */
477 if (strchr (name
, '/') == NULL
)
479 /* Search for NAME in several places. */
481 size_t namelen
= strlen (name
) + 1;
483 inline void trypath (const char *dirpath
)
485 fd
= open_path (name
, namelen
, dirpath
, &realname
);
490 /* First try the DT_RPATH of the dependent object that caused NAME
491 to be loaded. Then that object's dependent, and on up. */
492 for (l
= loader
; fd
== -1 && l
; l
= l
->l_loader
)
493 if (l
&& l
->l_info
[DT_RPATH
])
494 trypath ((const char *) (l
->l_addr
+
495 l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
+
496 l
->l_info
[DT_RPATH
]->d_un
.d_val
));
497 /* If dynamically linked, try the DT_RPATH of the executable itself. */
499 if (fd
== -1 && l
&& l
->l_type
!= lt_loaded
&& l
->l_info
[DT_RPATH
])
500 trypath ((const char *) (l
->l_addr
+
501 l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
+
502 l
->l_info
[DT_RPATH
]->d_un
.d_val
));
503 /* Try an environment variable (unless setuid). */
504 if (fd
== -1 && ! _dl_secure
)
505 trypath (getenv ("LD_LIBRARY_PATH"));
506 /* Finally, try the default path. */
509 extern const char *_dl_rpath
; /* Set in rtld.c. */
515 fd
= __open (name
, O_RDONLY
);
518 size_t len
= strlen (name
) + 1;
519 realname
= malloc (len
);
521 memcpy (realname
, name
, len
);
531 _dl_signal_error (errno
, name
, "cannot open shared object file");
533 return _dl_map_object_from_fd (name
, fd
, realname
, loader
, type
);