xref: /dflybsd-src/lib/libc/gen/elf_utils.c (revision fcf53d9b037c8b3b6b834245c8c0eb99b1458e72)
1*fcf53d9bSJohn Marino /*
2*fcf53d9bSJohn Marino  * Copyright (c) 2010 Konstantin Belousov <kib@freebsd.org>
3*fcf53d9bSJohn Marino  * All rights reserved.
4*fcf53d9bSJohn Marino  *
5*fcf53d9bSJohn Marino  * Redistribution and use in source and binary forms, with or without
6*fcf53d9bSJohn Marino  * modification, are permitted provided that the following conditions
7*fcf53d9bSJohn Marino  * are met:
8*fcf53d9bSJohn Marino  * 1. Redistributions of source code must retain the above copyright
9*fcf53d9bSJohn Marino  *    notice, this list of conditions and the following disclaimer.
10*fcf53d9bSJohn Marino  * 2. Neither the name of the author nor the names of any co-contributors
11*fcf53d9bSJohn Marino  *    may be used to endorse or promote products derived from this software
12*fcf53d9bSJohn Marino  *    without specific prior written permission.
13*fcf53d9bSJohn Marino  *
14*fcf53d9bSJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*fcf53d9bSJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*fcf53d9bSJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*fcf53d9bSJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*fcf53d9bSJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*fcf53d9bSJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*fcf53d9bSJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*fcf53d9bSJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*fcf53d9bSJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*fcf53d9bSJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*fcf53d9bSJohn Marino  * SUCH DAMAGE.
25*fcf53d9bSJohn Marino  *
26*fcf53d9bSJohn Marino  * $FreeBSD: head/lib/libc/gen/elf_utils.c 217154 2011-01-08 17:13:43Z kib $
27*fcf53d9bSJohn Marino  */
28*fcf53d9bSJohn Marino 
29*fcf53d9bSJohn Marino #include <link.h>
30*fcf53d9bSJohn Marino 
31*fcf53d9bSJohn Marino int
32*fcf53d9bSJohn Marino __elf_phdr_match_addr(struct dl_phdr_info *phdr_info, void *addr)
33*fcf53d9bSJohn Marino {
34*fcf53d9bSJohn Marino 	const Elf_Phdr *ph;
35*fcf53d9bSJohn Marino 	int i;
36*fcf53d9bSJohn Marino 
37*fcf53d9bSJohn Marino 	for (i = 0; i < phdr_info->dlpi_phnum; i++) {
38*fcf53d9bSJohn Marino 		ph = &phdr_info->dlpi_phdr[i];
39*fcf53d9bSJohn Marino 		if (ph->p_type != PT_LOAD || (ph->p_flags & PF_X) == 0)
40*fcf53d9bSJohn Marino 			continue;
41*fcf53d9bSJohn Marino 		if (phdr_info->dlpi_addr + ph->p_vaddr <= (uintptr_t)addr &&
42*fcf53d9bSJohn Marino 		    (uintptr_t)addr + sizeof(addr) < phdr_info->dlpi_addr +
43*fcf53d9bSJohn Marino 		    ph->p_vaddr + ph->p_memsz)
44*fcf53d9bSJohn Marino 			break;
45*fcf53d9bSJohn Marino 	}
46*fcf53d9bSJohn Marino 	return (i != phdr_info->dlpi_phnum);
47*fcf53d9bSJohn Marino }
48