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