about summary refs log tree commit diff
path: root/src/ldso/dynlink.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2012-01-23 02:02:59 -0500
committerRich Felker <dalias@aerifal.cx>2012-01-23 02:02:59 -0500
commite12fe65c928f038fb8db0caddcf535ab0a980c58 (patch)
tree0ad98e45dd5ba4213fe7f7e386669ba2bc7f2e2d /src/ldso/dynlink.c
parentc82f4a32ec694903a37d3f9e48da52486a470aa2 (diff)
downloadmusl-e12fe65c928f038fb8db0caddcf535ab0a980c58.tar.gz
musl-e12fe65c928f038fb8db0caddcf535ab0a980c58.tar.xz
musl-e12fe65c928f038fb8db0caddcf535ab0a980c58.zip
dynamic linker support for PIE binaries (position-independent main program)
even with this change, PIE will not work yet due to deficiencies in
the crt1.o startup code.
Diffstat (limited to 'src/ldso/dynlink.c')
-rw-r--r--src/ldso/dynlink.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/ldso/dynlink.c b/src/ldso/dynlink.c
index e1c2ad7d..df54230c 100644
--- a/src/ldso/dynlink.c
+++ b/src/ldso/dynlink.c
@@ -511,10 +511,13 @@ void *__dynlink(int argc, char **argv)
 		ehdr->e_phnum, ehdr->e_phentsize));
 	decode_dyn(lib);
 
-	/* Assume base address of 0 for the main program. This is not
-	 * valid for PIE code; we will have to search the PHDR to get
-	 * the correct load address in the PIE case (not yet supported). */
+	/* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
 	app->base = 0;
+	phdr = (void *)aux[AT_PHDR];
+	for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
+		if (phdr->p_type == PT_PHDR)
+			app->base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
+	}
 	app->name = argv[0];
 	app->global = 1;
 	app->dynv = (void *)(app->base + find_dyn(