2 * Copyright 1996-1998 John D. Polstra.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/param.h>
41 static Elf_Ehdr *get_elf_header(int, const char *);
42 static int convert_prot(int); /* Elf flags -> mmap protection */
43 static int convert_flags(int); /* Elf flags -> mmap flags */
46 * Map a shared object into memory. The "fd" argument is a file descriptor,
47 * which must be open on the object and positioned at its beginning.
48 * The "path" argument is a pathname that is used only for error messages.
50 * The return value is a pointer to a newly-allocated Obj_Entry structure
51 * for the shared object. Returns NULL on failure.
54 map_object(int fd, const char *path, const struct stat *sb)
82 size_t nclear, phsize;
92 hdr = get_elf_header(fd, path);
97 * Scan the program header entries, and save key information.
99 * We expect that the loadable segments are ordered by load address.
101 phdr = (Elf_Phdr *) ((char *)hdr + hdr->e_phoff);
102 phsize = hdr->e_phnum * sizeof (phdr[0]);
103 phlimit = phdr + hdr->e_phnum;
105 phdyn = phinterp = phtls = NULL;
111 segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
112 stack_flags = RTLD_DEFAULT_STACK_PF_EXEC | PF_R | PF_W;
113 while (phdr < phlimit) {
114 switch (phdr->p_type) {
121 segs[++nsegs] = phdr;
122 if ((segs[nsegs]->p_align & (PAGE_SIZE - 1)) != 0) {
123 _rtld_error("%s: PT_LOAD segment %d not page-aligned",
130 phdr_vaddr = phdr->p_vaddr;
131 phsize = phdr->p_memsz;
143 stack_flags = phdr->p_flags;
147 relro_page = phdr->p_vaddr;
148 relro_size = phdr->p_memsz;
152 if (phdr->p_offset > PAGE_SIZE ||
153 phdr->p_offset + phdr->p_filesz > PAGE_SIZE)
155 note_start = (Elf_Addr)(char *)hdr + phdr->p_offset;
156 note_end = note_start + phdr->p_filesz;
163 _rtld_error("%s: object is not dynamically-linked", path);
168 _rtld_error("%s: too few PT_LOAD segments", path);
173 * Map the entire address space of the object, to stake out our
174 * contiguous region, and to establish the base address for relocation.
176 base_vaddr = trunc_page(segs[0]->p_vaddr);
177 base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
178 mapsize = base_vlimit - base_vaddr;
179 base_addr = (caddr_t) base_vaddr;
180 base_flags = MAP_PRIVATE | MAP_ANON | MAP_NOCORE;
181 if (npagesizes > 1 && round_page(segs[0]->p_filesz) >= pagesizes[1])
182 base_flags |= MAP_ALIGNED_SUPER;
184 mapbase = mmap(base_addr, mapsize, PROT_NONE, base_flags, -1, 0);
185 if (mapbase == (caddr_t) -1) {
186 _rtld_error("%s: mmap of entire address space failed: %s",
187 path, rtld_strerror(errno));
190 if (base_addr != NULL && mapbase != base_addr) {
191 _rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
192 path, base_addr, mapbase);
196 for (i = 0; i <= nsegs; i++) {
197 /* Overlay the segment onto the proper region. */
198 data_offset = trunc_page(segs[i]->p_offset);
199 data_vaddr = trunc_page(segs[i]->p_vaddr);
200 data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
201 data_addr = mapbase + (data_vaddr - base_vaddr);
202 data_prot = convert_prot(segs[i]->p_flags);
203 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
204 if (mmap(data_addr, data_vlimit - data_vaddr, data_prot,
205 data_flags | MAP_PREFAULT_READ, fd, data_offset) == (caddr_t) -1) {
206 _rtld_error("%s: mmap of data failed: %s", path,
207 rtld_strerror(errno));
212 if (segs[i]->p_filesz != segs[i]->p_memsz) {
214 /* Clear any BSS in the last page of the segment. */
215 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
216 clear_addr = mapbase + (clear_vaddr - base_vaddr);
217 clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
219 if ((nclear = data_vlimit - clear_vaddr) > 0) {
220 /* Make sure the end of the segment is writable */
221 if ((data_prot & PROT_WRITE) == 0 && -1 ==
222 mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) {
223 _rtld_error("%s: mprotect failed: %s", path,
224 rtld_strerror(errno));
228 memset(clear_addr, 0, nclear);
230 /* Reset the data protection back */
231 if ((data_prot & PROT_WRITE) == 0)
232 mprotect(clear_page, PAGE_SIZE, data_prot);
235 /* Overlay the BSS segment onto the proper region. */
236 bss_vaddr = data_vlimit;
237 bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
238 bss_addr = mapbase + (bss_vaddr - base_vaddr);
239 if (bss_vlimit > bss_vaddr) { /* There is something to do */
240 if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
241 data_flags | MAP_ANON, -1, 0) == (caddr_t)-1) {
242 _rtld_error("%s: mmap of bss failed: %s", path,
243 rtld_strerror(errno));
249 if (phdr_vaddr == 0 && data_offset <= hdr->e_phoff &&
250 (data_vlimit - data_vaddr + data_offset) >=
251 (hdr->e_phoff + hdr->e_phnum * sizeof (Elf_Phdr))) {
252 phdr_vaddr = data_vaddr + hdr->e_phoff - data_offset;
258 obj->dev = sb->st_dev;
259 obj->ino = sb->st_ino;
261 obj->mapbase = mapbase;
262 obj->mapsize = mapsize;
263 obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
265 obj->vaddrbase = base_vaddr;
266 obj->relocbase = mapbase - base_vaddr;
267 obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
268 if (hdr->e_entry != 0)
269 obj->entry = (caddr_t) (obj->relocbase + hdr->e_entry);
270 if (phdr_vaddr != 0) {
271 obj->phdr = (const Elf_Phdr *) (obj->relocbase + phdr_vaddr);
273 obj->phdr = malloc(phsize);
274 if (obj->phdr == NULL) {
276 _rtld_error("%s: cannot allocate program header", path);
279 memcpy((char *)obj->phdr, (char *)hdr + hdr->e_phoff, phsize);
280 obj->phdr_alloc = true;
282 obj->phsize = phsize;
283 if (phinterp != NULL)
284 obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
286 tls_dtv_generation++;
287 obj->tlsindex = ++tls_max_index;
288 obj->tlssize = phtls->p_memsz;
289 obj->tlsalign = phtls->p_align;
290 obj->tlsinitsize = phtls->p_filesz;
291 obj->tlsinit = mapbase + phtls->p_vaddr;
293 obj->stack_flags = stack_flags;
294 obj->relro_page = obj->relocbase + trunc_page(relro_page);
295 obj->relro_size = round_page(relro_size);
296 if (note_start < note_end)
297 digest_notes(obj, note_start, note_end);
298 munmap(hdr, PAGE_SIZE);
302 munmap(mapbase, mapsize);
304 munmap(hdr, PAGE_SIZE);
309 get_elf_header(int fd, const char *path)
313 hdr = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE | MAP_PREFAULT_READ,
315 if (hdr == (Elf_Ehdr *)MAP_FAILED) {
316 _rtld_error("%s: read error: %s", path, rtld_strerror(errno));
320 /* Make sure the file is valid */
322 _rtld_error("%s: invalid file format", path);
325 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
326 hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
327 _rtld_error("%s: unsupported file layout", path);
330 if (hdr->e_ident[EI_VERSION] != EV_CURRENT ||
331 hdr->e_version != EV_CURRENT) {
332 _rtld_error("%s: unsupported file version", path);
335 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
336 _rtld_error("%s: unsupported file type", path);
339 if (hdr->e_machine != ELF_TARG_MACH) {
340 _rtld_error("%s: unsupported machine", path);
345 * We rely on the program header being in the first page. This is
346 * not strictly required by the ABI specification, but it seems to
347 * always true in practice. And, it simplifies things considerably.
349 if (hdr->e_phentsize != sizeof(Elf_Phdr)) {
351 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
354 if (hdr->e_phoff + hdr->e_phnum * sizeof(Elf_Phdr) >
356 _rtld_error("%s: program header too large", path);
362 munmap(hdr, PAGE_SIZE);
367 obj_free(Obj_Entry *obj)
372 free_tls_offset(obj);
373 while (obj->needed != NULL) {
374 Needed_Entry *needed = obj->needed;
375 obj->needed = needed->next;
378 while (!STAILQ_EMPTY(&obj->names)) {
379 Name_Entry *entry = STAILQ_FIRST(&obj->names);
380 STAILQ_REMOVE_HEAD(&obj->names, link);
383 while (!STAILQ_EMPTY(&obj->dldags)) {
384 elm = STAILQ_FIRST(&obj->dldags);
385 STAILQ_REMOVE_HEAD(&obj->dldags, link);
388 while (!STAILQ_EMPTY(&obj->dagmembers)) {
389 elm = STAILQ_FIRST(&obj->dagmembers);
390 STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
395 if (obj->origin_path)
396 free(obj->origin_path);
404 free((void *)obj->phdr);
413 obj = CNEW(Obj_Entry);
414 STAILQ_INIT(&obj->dldags);
415 STAILQ_INIT(&obj->dagmembers);
416 STAILQ_INIT(&obj->names);
421 * Given a set of ELF protection flags, return the corresponding protection
425 convert_prot(int elfflags)
438 convert_flags(int elfflags)
440 int flags = MAP_PRIVATE; /* All mappings are private */
443 * Readonly mappings are marked "MAP_NOCORE", because they can be
444 * reconstructed by a debugger.
446 if (!(elfflags & PF_W))