summaryrefslogtreecommitdiff
path: root/system/musl/handle-aux-at_base.patch
blob: 697c82bbb46387c6a3d1d33b17bc4282d52c3287 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
diff --git a/src/env/__init_tls.c b/src/env/__init_tls.c
index b125eb1..616c6a6 100644
--- a/src/env/__init_tls.c
+++ b/src/env/__init_tls.c
@@ -72,8 +72,10 @@ void *__copy_tls(unsigned char *mem)
 }
 
 #if ULONG_MAX == 0xffffffff
+typedef Elf32_Ehdr Ehdr;
 typedef Elf32_Phdr Phdr;
 #else
+typedef Elf64_Ehdr Ehdr;
 typedef Elf64_Phdr Phdr;
 #endif
 
@@ -82,15 +84,23 @@ extern const size_t _DYNAMIC[];
 static void static_init_tls(size_t *aux)
 {
 	unsigned char *p;
-	size_t n;
+	size_t n, e;
 	Phdr *phdr, *tls_phdr=0;
 	size_t base = 0;
 	void *mem;
 
-	for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
+	if (aux[AT_BASE]) {
+		Ehdr *ehdr = (void *)aux[AT_BASE];
+		p = (unsigned char *)aux[AT_BASE] + ehdr->e_phoff;
+		n = ehdr->e_phnum;
+		e = ehdr->e_phentsize;
+	} else {
+		p = (void *)aux[AT_PHDR];
+		n = aux[AT_PHNUM];
+		e = aux[AT_PHENT];
+	}
+	for (; n; n--, p+=e) {
 		phdr = (void *)p;
-		if (phdr->p_type == PT_PHDR)
-			base = aux[AT_PHDR] - phdr->p_vaddr;
 		if (phdr->p_type == PT_DYNAMIC && _DYNAMIC)
 			base = (size_t)_DYNAMIC - phdr->p_vaddr;
 		if (phdr->p_type == PT_TLS)