Index: uspace/srv/loader/Makefile
===================================================================
--- uspace/srv/loader/Makefile	(revision c53d90624940aafb20634e3c830f61e7551afe43)
+++ uspace/srv/loader/Makefile	(revision 2ca5f6326f12aee981cdb0aa046f8e37cd7d9a67)
@@ -39,5 +39,5 @@
 LINKER_SCRIPT = $(LIBC_PREFIX)/arch/$(UARCH)/_link-loader.ld
 
-EXTRA_CFLAGS = -Iinclude
+EXTRA_CFLAGS = -Iinclude -I../../lib/c/rtld/include
 
 BINARY = loader
Index: uspace/srv/loader/elf_load.c
===================================================================
--- uspace/srv/loader/elf_load.c	(revision c53d90624940aafb20634e3c830f61e7551afe43)
+++ uspace/srv/loader/elf_load.c	(revision 2ca5f6326f12aee981cdb0aa046f8e37cd7d9a67)
@@ -103,5 +103,6 @@
  *
  */
-int elf_load_file(const char *file_name, size_t so_bias, elf_info_t *info)
+int elf_load_file(const char *file_name, size_t so_bias, eld_flags_t flags,
+    elf_info_t *info)
 {
 	elf_ld_t elf;
@@ -118,4 +119,5 @@
 	elf.fd = fd;
 	elf.info = info;
+	elf.flags = flags;
 
 	rc = elf_load(&elf, so_bias);
@@ -124,19 +126,4 @@
 
 	return rc;
-}
-
-/** Run an ELF executable.
- *
- * Transfers control to the entry point of an ELF executable loaded
- * earlier with elf_load_file(). This function does not return.
- *
- * @param info Info structure filled earlier by elf_load_file()
- *
- */
-void elf_run(elf_info_t *info, pcb_t *pcb)
-{
-	entry_point_jmp(info->entry, pcb);
-
-	/* not reached */
 }
 
@@ -153,4 +140,5 @@
 	pcb->entry = info->entry;
 	pcb->dynamic = info->dynamic;
+	pcb->rtld_runtime = NULL;
 }
 
@@ -306,11 +294,20 @@
 		break;
 	case PT_INTERP:
-		/* Assume silently interp == "/rtld.so" */
-		elf->info->interp = "/rtld.so";
+		/* Assume silently interp == "/app/dload" */
+		elf->info->interp = "/app/dload";
 		break;
 	case PT_DYNAMIC:
+		/* Record pointer to dynamic section into info structure */
+		elf->info->dynamic =
+		    (void *)((uint8_t *)entry->p_vaddr + elf->bias);
+		DPRINTF("dynamic section found at 0x%x\n",
+			(uintptr_t)elf->info->dynamic);
+		break;
+	case 0x70000000:
+		/* FIXME: MIPS reginfo */
+		break;
 	case PT_SHLIB:
-	case PT_LOPROC:
-	case PT_HIPROC:
+//	case PT_LOPROC:
+//	case PT_HIPROC:
 	default:
 		DPRINTF("Segment p_type %d unknown.\n", entry->p_type);
@@ -383,5 +380,6 @@
 	    AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE);
 	if (a == (void *)(-1)) {
-		DPRINTF("Memory mapping failed.\n");
+		DPRINTF("memory mapping failed (0x%x, %d)\n",
+			base+bias, mem_sz);
 		return EE_MEMORY;
 	}
@@ -425,4 +423,11 @@
 	}
 
+	/*
+	 * The caller wants to modify the segments first. He will then
+	 * need to set the right access mode and ensure SMC coherence.
+	 */
+	if ((elf->flags & ELDF_RW) != 0) return EE_OK;
+
+//	printf("set area flags to %d\n", flags);
 	rc = as_area_change_flags(seg_ptr, flags);
 	if (rc != 0) {
Index: uspace/srv/loader/include/elf_load.h
===================================================================
--- uspace/srv/loader/include/elf_load.h	(revision c53d90624940aafb20634e3c830f61e7551afe43)
+++ uspace/srv/loader/include/elf_load.h	(revision 2ca5f6326f12aee981cdb0aa046f8e37cd7d9a67)
@@ -43,4 +43,9 @@
 #include "elf.h"
 
+typedef enum {
+	/** Leave all segments in RW access mode. */
+	ELDF_RW = 1
+} eld_flags_t;
+
 /**
  * Some data extracted from the headers are stored here
@@ -67,4 +72,7 @@
 	uintptr_t bias;
 
+	/** Flags passed to the ELF loader. */
+	eld_flags_t flags;
+
 	/** A copy of the ELF file header */
 	elf_header_t *header;
@@ -74,6 +82,6 @@
 } elf_ld_t;
 
-int elf_load_file(const char *file_name, size_t so_bias, elf_info_t *info);
-void elf_run(elf_info_t *info, pcb_t *pcb);
+int elf_load_file(const char *file_name, size_t so_bias, eld_flags_t flags,
+    elf_info_t *info);
 void elf_create_pcb(elf_info_t *info, pcb_t *pcb);
 
Index: uspace/srv/loader/main.c
===================================================================
--- uspace/srv/loader/main.c	(revision c53d90624940aafb20634e3c830f61e7551afe43)
+++ uspace/srv/loader/main.c	(revision 2ca5f6326f12aee981cdb0aa046f8e37cd7d9a67)
@@ -55,4 +55,5 @@
 #include <macros.h>
 #include <loader/pcb.h>
+#include <entry_point.h>
 #include <errno.h>
 #include <async.h>
@@ -63,5 +64,13 @@
 #include <elf_load.h>
 
+/* From librtld */
+#include <rtld.h>
+#include <dynamic.h>
+#include <elf_load.h>
+#include <module.h>
+
 #define DPRINTF(...)
+
+static int ldr_load_dyn_linked(elf_info_t *p_info);
 
 /** Pathname of the file that will be loaded */
@@ -89,10 +98,11 @@
 
 static elf_info_t prog_info;
-static elf_info_t interp_info;
-
-static bool is_dyn_linked;
 
 /** Used to limit number of connections to one. */
 static bool connected = false;
+
+/** State structure of the dynamic linker. */
+runtime_env_t dload_re;
+static module_t prog_mod;
 
 static void ldr_get_taskid(ipc_callid_t rid, ipc_call_t *request)
@@ -283,5 +293,5 @@
 	int rc;
 	
-	rc = elf_load_file(pathname, 0, &prog_info);
+	rc = elf_load_file(pathname, 0, 0, &prog_info);
 	if (rc != EE_OK) {
 		DPRINTF("Failed to load executable '%s'.\n", pathname);
@@ -302,23 +312,64 @@
 	if (prog_info.interp == NULL) {
 		/* Statically linked program */
-		is_dyn_linked = false;
 		async_answer_0(rid, EOK);
 		return 0;
 	}
 	
-	rc = elf_load_file(prog_info.interp, 0, &interp_info);
-	if (rc != EE_OK) {
-		DPRINTF("Failed to load interpreter '%s.'\n",
-		    prog_info.interp);
-		async_answer_0(rid, EINVAL);
-		return 1;
-	}
-	
-	is_dyn_linked = true;
-	async_answer_0(rid, EOK);
-	
+	DPRINTF("Binary is dynamically linked.\n");
+	DPRINTF(" - pcb address: %p\n", &pcb);
+	DPRINTF( "- prog dynamic: %p\n", prog_info.dynamic);
+
+	rc = ldr_load_dyn_linked(&prog_info);
+
+	async_answer_0(rid, rc);
 	return 0;
 }
 
+static int ldr_load_dyn_linked(elf_info_t *p_info)
+{
+	runtime_env = &dload_re;
+
+	DPRINTF("Load dynamically linked program.\n");
+
+	/*
+	 * First we need to process dynamic sections of the executable
+	 * program and insert it into the module graph.
+	 */
+
+	DPRINTF("Parse program .dynamic section at %p\n", p_info->dynamic);
+	dynamic_parse(p_info->dynamic, 0, &prog_mod.dyn);
+	prog_mod.bias = 0;
+	prog_mod.dyn.soname = "[program]";
+
+	/* Initialize list of loaded modules */
+	list_initialize(&runtime_env->modules_head);
+	list_append(&prog_mod.modules_link, &runtime_env->modules_head);
+
+	/* Pointer to program module. Used as root of the module graph. */
+	runtime_env->program = &prog_mod;
+
+	/* Work around non-existent memory space allocation. */
+	runtime_env->next_bias = 0x1000000;
+
+	/*
+	 * Now we can continue with loading all other modules.
+	 */
+
+	DPRINTF("Load all program dependencies\n");
+	module_load_deps(&prog_mod);
+
+	/*
+	 * Now relocate/link all modules together.
+	 */
+
+	/* Process relocations in all modules */
+	DPRINTF("Relocate all modules\n");
+	modules_process_relocs(&prog_mod);
+
+	/* Pass runtime evironment pointer through PCB. */
+	pcb.rtld_runtime = (void *) runtime_env;
+
+	return 0;
+}
 
 /** Run the previously loaded program.
@@ -332,4 +383,6 @@
 	const char *cp;
 	
+	DPRINTF("Set task name\n");
+
 	/* Set the task name. */
 	cp = str_rchr(pathname, '/');
@@ -337,16 +390,9 @@
 	task_set_name(cp);
 	
-	if (is_dyn_linked == true) {
-		/* Dynamically linked program */
-		DPRINTF("Run ELF interpreter.\n");
-		DPRINTF("Entry point: %p\n", interp_info.entry);
-		
-		async_answer_0(rid, EOK);
-		elf_run(&interp_info, &pcb);
-	} else {
-		/* Statically linked program */
-		async_answer_0(rid, EOK);
-		elf_run(&prog_info, &pcb);
-	}
+	/* Run program */
+	DPRINTF("Reply OK\n");
+	async_answer_0(rid, EOK);
+	DPRINTF("Jump to entry point at %p\n", pcb.entry);
+	entry_point_jmp(prog_info.entry, &pcb);
 	
 	/* Not reached */
