about summary refs log tree commit diff
path: root/src/ldso/dl_iterate_phdr.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2012-10-31 21:27:48 -0400
committerRich Felker <dalias@aerifal.cx>2012-10-31 21:27:48 -0400
commit18c0e02e2bd53ceedbb843b06ff90890f1c734b0 (patch)
tree37c29715025d24e38415e72ac8ec896fb22a2dcf /src/ldso/dl_iterate_phdr.c
parent76f28cfce59dfc499252c874f87c34567e6c86c6 (diff)
downloadmusl-18c0e02e2bd53ceedbb843b06ff90890f1c734b0.tar.gz
musl-18c0e02e2bd53ceedbb843b06ff90890f1c734b0.tar.xz
musl-18c0e02e2bd53ceedbb843b06ff90890f1c734b0.zip
add dl_iterate_phdr interface
patches by Alex Caudill (npx). the dynamic-linked version is almost
identical to the final submitted patch; I just added a couple missing
lines for saving the phdr address when the dynamic linker is invoked
directly to run a program, and removed a couple to avoid introducing
another unnecessary type. the static-linked version is based on npx's
draft. it could use some improvements which are contingent on the
startup code saving some additional information for later use.
Diffstat (limited to 'src/ldso/dl_iterate_phdr.c')
-rw-r--r--src/ldso/dl_iterate_phdr.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/ldso/dl_iterate_phdr.c b/src/ldso/dl_iterate_phdr.c
new file mode 100644
index 00000000..49b321a0
--- /dev/null
+++ b/src/ldso/dl_iterate_phdr.c
@@ -0,0 +1,43 @@
+#ifndef SHARED
+
+#include <elf.h>
+#include <link.h>
+#include "libc.h"
+
+#define AUX_CNT 38
+
+int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void *data), void *data)
+{
+	unsigned char *p;
+	ElfW(Phdr) *phdr, *tls_phdr=0;
+	size_t base = 0;
+	size_t n;
+	struct dl_phdr_info info;
+	size_t i, aux[AUX_CNT];
+
+	for (i=0; libc.auxv[i]; i+=2)
+		if (libc.auxv[i]<AUX_CNT) aux[libc.auxv[i]] = libc.auxv[i+1];
+
+	for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
+		phdr = (void *)p;
+		if (phdr->p_type == PT_PHDR)
+			base = aux[AT_PHDR] - phdr->p_vaddr;
+		if (phdr->p_type == PT_TLS)
+			tls_phdr = phdr;
+	}
+	info.dlpi_addr  = base;
+	info.dlpi_name  = "/proc/self/exe";
+	info.dlpi_phdr  = (void *)aux[AT_PHDR];
+	info.dlpi_phnum = aux[AT_PHNUM];
+	info.dlpi_adds  = 0;
+	info.dlpi_subs  = 0;
+	if (tls_phdr) {
+		info.dlpi_tls_modid = 1;
+		info.dlpi_tls_data = (void *)(base + tls_phdr->p_vaddr);
+	} else {
+		info.dlpi_tls_modid = 0;
+		info.dlpi_tls_data = 0;
+	}
+	return (callback)(&info, sizeof (info), data);
+}
+#endif