Permalink
Browse files

not quite working yet

  • Loading branch information...
1 parent dbec06c commit f10bb600be4f0e0c41a9a7b5d73a00d84b6425cd @nickbjohnson4224 committed Dec 9, 2011
Showing with 54 additions and 81 deletions.
  1. +1 −1 Makefile
  2. +1 −0 dl/dl.h
  3. +32 −2 dl/elfc.c
  4. +16 −4 dl/exec.c
  5. +3 −11 fish/main.c
  6. +0 −43 libtest/Makefile
  7. +0 −19 libtest/test.c
  8. +1 −1 util/echo/Makefile
View
@@ -1,6 +1,6 @@
BUILDDIR=$(PWD)
-LIB_DIRS = libc dl librdi libtest ports/lua ports/freetype libtoolkit
+LIB_DIRS = libc dl librdi ports/lua ports/freetype libtoolkit
DRIVERS := $(shell find driver -mindepth 1 -maxdepth 1)
DAEMONS := $(shell find daemon -mindepth 1 -maxdepth 1)
View
@@ -75,6 +75,7 @@ void elf_gencache(struct elf_cache *cache, const struct elf32_ehdr *image);
uint32_t elfc_resolve (struct elf_cache *cache, const char *symbol);
uint32_t elfc_relocate (struct elf_cache *cache, const struct elf32_rel *rel);
void elfc_relocate_all (struct elf_cache *cache);
+void elfc_relocate_exec(struct elf_cache *cache);
int dl_enter(void *entry_ptr);
View
@@ -78,6 +78,11 @@ uint32_t elfc_relocate(struct elf_cache *cache, const struct elf32_rel *rel) {
void elfc_relocate_all(struct elf_cache *cache) {
size_t i;
+
+ /* only relocate dynamic objects */
+ if (!cache->dynamic) {
+ return;
+ }
/* add special entries to GOT for PLT resolution */
((uint32_t*) cache->pltgot)[1] = (uint32_t) cache->image;
@@ -98,6 +103,26 @@ void elfc_relocate_all(struct elf_cache *cache) {
}
}
+void elfc_relocate_exec(struct elf_cache *cache) {
+ size_t i;
+
+ /* only relocate dynamic objects */
+ if (!cache->dynamic) {
+ return;
+ }
+
+ /* add special entries to GOT for PLT resolution */
+ ((uint32_t*) cache->pltgot)[1] = (uint32_t) cache->image;
+ ((uint32_t*) cache->pltgot)[2] = (uint32_t) __plt_resolve;
+
+ /* perform normal relocations (data) */
+ if (cache->reltab) {
+ for (i = 0; i < cache->reltabn; i++) {
+ elfc_relocate(cache, &cache->reltab[i]);
+ }
+ }
+}
+
/*****************************************************************************
* elfc_get_needed
*
@@ -282,7 +307,7 @@ uint32_t elfc_resolve(struct elf_cache *cache, const char *symbol) {
*/
void elf_gencache(struct elf_cache *cache, const struct elf32_ehdr *image) {
- uintptr_t base = (uintptr_t) image;
+ uint32_t base = (uint32_t) image;
size_t i;
cache->image = image;
@@ -292,16 +317,21 @@ void elf_gencache(struct elf_cache *cache, const struct elf32_ehdr *image) {
/* calulate image size */
for (i = 0; cache->segtab[i].p_type == PT_LOAD || cache->segtab[i].p_type == PT_DYNAMIC; i++);
- cache->vsize = cache->segtab[i - 1].p_vaddr + cache->segtab[i - 1].p_memsz;
+ if (i) cache->vsize = cache->segtab[i - 1].p_vaddr + cache->segtab[i - 1].p_memsz;
/* get DYNAMIC segment */
+ cache->dynamic = NULL;
for (i = 0; cache->segtab[i].p_type != PT_NULL; i++) {
if (cache->segtab[i].p_type == PT_DYNAMIC) {
cache->dynamic = (const struct elf32_dyn*) (base + cache->segtab[i].p_offset);
break;
}
}
+ if (!cache->dynamic) {
+ return;
+ }
+
/* get various DYNAMIC values */
cache->symtab = NULL;
cache->reltab = NULL;
View
@@ -27,6 +27,8 @@ int _exec(void *image, size_t size, int flags) {
struct slt32_header *slt_hdr;
struct slt32_entry *slt;
struct elf32_ehdr *exec;
+ void *temp;
+ struct elf_cache cache;
void *entry;
size_t i;
@@ -38,7 +40,7 @@ int _exec(void *image, size_t size, int flags) {
/*** POINT OF MAYBE RETURNING IF YOU, y'know, have to... ***/
/* copy executable high */
- exec = (void*) sltalloc("dl.img.exec", size);
+ temp = exec = (void*) sltalloc("dl.img.exec", size);
if ((uintptr_t) image % PAGESZ) {
/* not aligned, copy */
@@ -63,11 +65,21 @@ int _exec(void *image, size_t size, int flags) {
}
/* load executable */
- elf_load(exec, 0);
- entry = (void*) exec->e_entry;
+ if (exec->e_type == ET_EXEC) {
+ elf_load(exec, 0);
+ exec = (void*) 0x100000;
+ entry = (void*) exec->e_entry;
+ }
+ else {
+ elf_load(exec, 0x100000);
+ entry = (void*) (exec->e_entry + 0x100000);
+
+ elf_gencache(&cache, exec);
+ elfc_relocate_all(&cache);
+ }
/* remove executable image */
- page_free(exec, size);
+ page_free(temp, size);
sltfree_name("dl.img.exec");
/* reset event handler */
View
@@ -136,18 +136,10 @@ int main() {
size_t i, n;
char *argv[100];
- uint32_t *object0, *object1;
- const char *symbol = "baz";
+ uint32_t *object;
- object0 = dlopen("/lib/libc.so", 0);
- printf("libc.so:\t%p\n", object0);
-
- object1 = dlopen("/lib/libtest.so", 0);
- printf("libtest.so:\t%p\n", object1);
-
- printf("libtest.so:__zab\t%p\n", dlsym(object1, "__zab"));
-
- printf("%d\n", ((int (*)(int)) dlsym(object1, symbol))(7));
+ object = dlopen("/lib/libc.so", 0);
+ printf("libc.so:\t%p\n", object);
setenv("PWD", "/");
View
@@ -1,43 +0,0 @@
-SOURCES := $(patsubst %.c,%.o,$(shell find . -name "*.c"))
-SOURCES += $(patsubst %.s,%.o,$(shell find . -name "*.s"))
-HEADERS := $(shell find inc -name "*.h")
-SUBDIR := libtest
-STATIC := libtest.a
-SHARED := libtest.so
-CFLAGS += -I$(BUILDDIR)/$(SUBDIR)/inc
-ASFLAGS := -felf
-
-.PHONY: headers_dir $(HEADERS) clean
-
-all: $(STATIC) $(SHARED)
-
-headers_dir:
- @ echo " MKDIR " $(BUILDDIR)/inc/rdi
- @ mkdir -p $(BUILDDIR)/inc/rdi
-
-$(HEADERS): headers_dir
- @ echo " CP " $(SUBDIR)/$@ "->" $(BUILDDIR)/inc
- @ cp $@ $(BUILDDIR)/$@
-
-$(STATIC): $(HEADERS) $(SOURCES)
- @ echo " AR " $(SUBDIR)/$(STATIC)
- @ $(AR) $(ARFLAGS) $(STATIC) $(SOURCES)
- @ echo " CP " $(SUBDIR)/$(STATIC) "->" $(BUILDDIR)/lib
- @ cp $(STATIC) $(BUILDDIR)/lib
-
-$(SHARED): $(HEADERS) $(SOURCES)
- @ echo " LD " $(SUBDIR)/$(SHARED)
- @ $(LD) $(LDFLAGS) -shared $(SOURCES) -o $(SHARED) -Bdynamic -lc -soname libtest.so.1
- @ echo " CP " $(SUBDIR)/$(SHARED) "->" $(BUILDDIR)/lib
- @ cp $(SHARED) $(BUILDDIR)/lib
-
-%.o: %.s
- @ echo " AS " $(SUBDIR)/$<
- @ $(AS) $(ASFLAGS) $<
-
-%.o: %.c
- @ echo " CC " $(SUBDIR)/$<
- @ $(CC) $(CFLAGS) -c $< -o $@
-
-clean:
- @ rm $(TARGET) $(SOURCES)
View
@@ -1,19 +0,0 @@
-#include <stdlib.h>
-
-int foo = 42;
-
-int bar(int x) __attribute__ ((noinline));
-int bar(int x) {
- int y = foo;
- foo = x;
- return y;
-}
-
-int bar1(int x) {
- return 1 + bar(x - 1);
-}
-
-int __zab(int);
-int baz(int x) {
- return __zab(x + 1);
-}
View
@@ -4,7 +4,7 @@ SOURCES += $(BUILDDIR)/lib/c0.o
SUBDIR := util/echo
TARGET := echo
CFLAGS += -I$(BUILDDIR)/$(SUBDIR)/inc
-LDFLAGS += -lc
+LDFLAGS += -lc -pie
ASFLAGS := -felf
all: $(TARGET)

0 comments on commit f10bb60

Please sign in to comment.