diff options
author | Gwenolé Beauchesne <gbeauchesne@mandriva.org> | 2003-06-04 18:31:57 +0000 |
---|---|---|
committer | Gwenolé Beauchesne <gbeauchesne@mandriva.org> | 2003-06-04 18:31:57 +0000 |
commit | 09e967c2d732783b2579e4e120cd9b608404cb00 (patch) | |
tree | 8d2783a6a7e33608c6012efd6a88b8f5694df81d /mdk-stage1/dietlibc/libdl | |
parent | 18fcff49d3c836697d3b75a3d01d31c700e69974 (diff) | |
download | drakx-09e967c2d732783b2579e4e120cd9b608404cb00.tar drakx-09e967c2d732783b2579e4e120cd9b608404cb00.tar.gz drakx-09e967c2d732783b2579e4e120cd9b608404cb00.tar.bz2 drakx-09e967c2d732783b2579e4e120cd9b608404cb00.tar.xz drakx-09e967c2d732783b2579e4e120cd9b608404cb00.zip |
Merge from R9_0-AMD64, most notably:
- AMD64 support to insmod-busybox, minilibc, et al.
- Sync with insmod-modutils 2.4.19 something but everyone should use
dietlibc nowadays
- Factor out compilation and prefix with $(DIET) for dietlibc builds
- 64-bit & varargs fixes
Diffstat (limited to 'mdk-stage1/dietlibc/libdl')
-rw-r--r-- | mdk-stage1/dietlibc/libdl/Makefile | 32 | ||||
-rw-r--r-- | mdk-stage1/dietlibc/libdl/README | 82 | ||||
-rw-r--r-- | mdk-stage1/dietlibc/libdl/_dl_int.h | 138 | ||||
-rw-r--r-- | mdk-stage1/dietlibc/libdl/_dl_jump.S | 53 | ||||
-rw-r--r-- | mdk-stage1/dietlibc/libdl/_dl_open.c | 228 | ||||
-rw-r--r-- | mdk-stage1/dietlibc/libdl/_dl_search.c | 81 | ||||
-rw-r--r-- | mdk-stage1/dietlibc/libdl/dlclose.c | 40 | ||||
-rw-r--r-- | mdk-stage1/dietlibc/libdl/dlerror.c | 57 | ||||
-rw-r--r-- | mdk-stage1/dietlibc/libdl/dlopen.c | 31 | ||||
-rw-r--r-- | mdk-stage1/dietlibc/libdl/dlsym.c | 93 |
10 files changed, 556 insertions, 279 deletions
diff --git a/mdk-stage1/dietlibc/libdl/Makefile b/mdk-stage1/dietlibc/libdl/Makefile index c0463f887..29164f105 100644 --- a/mdk-stage1/dietlibc/libdl/Makefile +++ b/mdk-stage1/dietlibc/libdl/Makefile @@ -1,24 +1,38 @@ # local "test" make file -SRC = $(wildcard *.c) test/test.c +CC = gcc +DCC = diet $(CC) + +SRC = $(wildcard *.c) # test/test.c OBJ = _dl_jump.o $(SRC:.c=.o) -#CFLAGS = -Wall -O2 -CFLAGS = -g +#CFLAGS = -Wall -D__OD_CLEAN_ROOM -DNODIETREF -fPIC -g -O +CFLAGS = -Wall -O2 -D__OD_CLEAN_ROOM -DNODIETREF -fPIC -frerun-loop-opt -fschedule-insns2 $(DEB) # -fomit-frame-pointer %.o : %.S - $(HOME)/bin/diet gcc $(CFLAGS) -c -o $@ $^ + $(DCC) $(CFLAGS) -c -o $@ $^ %.o : %.c - $(HOME)/bin/diet gcc $(CFLAGS) -c -o $@ $^ + $(DCC) $(CFLAGS) -c -o $@ $^ + +it: t.out + +a.out: t.out + gcc -O -g test/a.c t.out -L/opt/diet/lib-i386 -lc -lgcc -o $@ -Wl,-dynamic-linker=`pwd`/t.out -nostdlib + +t.out: _dl_main.o + ld -shared -o $@ $? -soname=libdl.so + +all: test.out libdl.a -all: test.out +libdl.a: $(OBJ) + ar cru $@ $^ -test.g: test.c +test.g: test/test.c gcc -o $@ $^ -ldl -test.out: $(OBJ) - $(HOME)/bin/diet gcc -o $@ $^ +test.out: $(OBJ) test/test.o + diet gcc -o $@ $^ clean: $(RM) *.o *.a *.out *~ # *.so diff --git a/mdk-stage1/dietlibc/libdl/README b/mdk-stage1/dietlibc/libdl/README index 5d77bde34..6f1d0e46e 100644 --- a/mdk-stage1/dietlibc/libdl/README +++ b/mdk-stage1/dietlibc/libdl/README @@ -1,6 +1,82 @@ -this is the experimental libdl test-pre-pre-alpha code +this is the experimental libdl written by Olaf Dreesen -Shouldn't work yet had some problems with the elf specs... -and more inconsistencies... +Fri Nov 15 16:23:28 CET 2002 + + fixing the copy-relocation bug. + + TODO as last time.... plus + - dlsym with RTLD_NEXT + +Wed Nov 6 19:08:00 CET 2002 + + Ups.. misunderstood the manual-page of dlopen. + now dlopen(0,...) works the way it is described in the susv2, BUT still not + complete. + + TODO: + - gdb support + + - RTLD_GLOBAL/RTLD_LOCAL + at the moment ALL are GLOBAL. + + - open of a shared object ONCE + there is no library hard-, symlink detection yet. + + - code for other archs then i386 and arm + + +Fri Sep 13 16:28:42 CEST 2002 + + this unified libdl/dynlinker is now the default. + + +Mon Mar 11 19:44:20 CET 2002 + + the unified libdl/dynlinker now seems to stand the NORMAL usage so I think + it should be tested outside my CLEANROOM :) + + TODO: code for other archs than i386 and arm + + +Thu Nov 1 18:41:36 MET 2001 + + ufffff.... now the libdl code can be used in the libdl.so :) + Status: it works in my clean room on i386, arm-code started + + TODO: see below :) + + +Wed Jul 4 11:55:00 MEST 2001 + + Uwe Ohse pointed to some flaws in my code, I have started to fix them.... + + TODO: rebuild the code in a more readable and consistent way. + TODO: code for other archs than i386 + + +Thu Jun 21 15:32:45 MEST 2001 + + It work good enough to make a libdietc.so possible. + + TODO: rebuild the code in a more readable and consistent way. + TODO: code for other archs than i386 + + +Thu Jun 7 14:10:03 MEST 2001 + + Status: test-pre-alpha code + the code is now "testable" ... + I need a dyn-dietlibc specific ld.so. (in work) + + TODO: rebuild the code in a more readable and consistent way. + + +INITIAL May ? : + + Status: test-pre-pre-alpha code + + Shouldn't work yet. Had and have some problems with the elf specs... + and more inconsistencies... + diff --git a/mdk-stage1/dietlibc/libdl/_dl_int.h b/mdk-stage1/dietlibc/libdl/_dl_int.h index 659e6ee0f..1bab10a92 100644 --- a/mdk-stage1/dietlibc/libdl/_dl_int.h +++ b/mdk-stage1/dietlibc/libdl/_dl_int.h @@ -1,31 +1,151 @@ #ifndef ___DL_INT_H__ #define ___DL_INT_H__ +#if defined(__alpha__) || defined(__sparc64__) || defined(__x86_64__) +#define ELF_CLASS ELFCLASS64 +#else +#define ELF_CLASS ELFCLASS32 +#endif + #include <elf.h> +#include <dlfcn.h> + +#include "_dl_rel.h" + +#if ELF_CLASS == ELFCLASS32 + +#define Elf_Addr Elf32_Addr + +#define Elf_Dyn Elf32_Dyn +#define Elf_Ehdr Elf32_Ehdr +#define Elf_Phdr Elf32_Phdr +#define Elf_Rel Elf32_Rel +#define Elf_Rela Elf32_Rela +#define Elf_Sym Elf32_Sym + +#define ELF_R_SYM(x) ELF32_R_SYM((x)) +#define ELF_R_TYPE(x) ELF32_R_TYPE((x)) + +#define ELF_ST_BIND(x) ELF32_ST_BIND((x)) +#define ELF_ST_TYPE(x) ELF32_ST_TYPE((x)) + +#else + +#define Elf_Addr Elf64_Addr + +#define Elf_Dyn Elf64_Dyn +#define Elf_Ehdr Elf64_Ehdr +#define Elf_Phdr Elf64_Phdr +#define Elf_Rel Elf64_Rel +#define Elf_Rela Elf64_Rela +#define Elf_Sym Elf64_Sym + +#define ELF_R_SYM(x) ELF64_R_SYM((x)) +#define ELF_R_TYPE(x) ELF64_R_TYPE((x)) + +#define ELF_ST_BIND(x) ELF64_ST_BIND((x)) +#define ELF_ST_TYPE(x) ELF64_ST_TYPE((x)) + +#endif + +//#define DEBUG +#ifdef DEBUG +#ifndef __DIET_LD_SO__ +#include <stdio.h> +#define pf(s) write(2,s,strlen(s)) +#define ph(l) fdprintf(2,"%08lx",(l)) +#endif +#endif + +#define RTLD_USER 0x10000000 +#define RTLD_NOSONAME 0x20000000 +#define LDSO_FLAGS (RTLD_LAZY|RTLD_GLOBAL|RTLD_NOSONAME) struct _dl_handle { + char * mem_base; /* base address of maped *.so / or zero if program | Elf_Addr l_addr */ + char * l_name; /* Abloslute filename of this object */ + Elf_Dyn* dynamic; /* _DYNAMIC */ + + struct _dl_handle *next; + struct _dl_handle *prev; + + unsigned int flags; /* FLAGS */ + + char * name; /* name of shared object */ + /* basic */ - void * mem_base; /* base address of maped *.so */ unsigned long mem_size; /* len of mem block */ - unsigned int lnk_count; /* reference count */ + unsigned long lnk_count; /* reference count (other libraries) */ + + /* lazy evaluator data */ + unsigned long*pltgot; /* PLT/GOT */ /* symbol resolve helper */ unsigned long*hash_tab; /* hash table */ - Elf32_Sym * dyn_sym_tab; /* dynamic symbol table */ char * dyn_str_tab; /* dyn_name table */ + Elf_Sym * dyn_sym_tab; /* dynamic symbol table */ + _dl_rel_t* plt_rel; /* PLT relocation table */ - Elf32_Rel * plt_rel; /* PLT relocation table */ - - /* FINI */ + /* INIT / FINI */ void (*fini)(void); }; -#define HASH_BUCKET_LEN(p) ((p)) +/* debug communication (GDB) (dyn-linker only) */ +struct r_debug { + int r_version; + struct _dl_handle* r_map; + unsigned long r_brk; + enum { + RT_CONSISTENT, /* mapping complete */ + RT_ADD, /* begin add object */ + RT_DELETE, /* begin del object */ + } r_state; + unsigned long r_ldbase; +}; + +#define HASH_BUCKET_LEN(p) (*((p))) #define HASH_BUCKET(p) ((p)+2) -#define HASH_CHAIN_LEN(p) ((p)+1) +#define HASH_CHAIN_LEN(p) (*((p)+1)) #define HASH_CHAIN(p) ((p)+2+HASH_BUCKET_LEN(p)) -unsigned long elf_hash(const unsigned char *name); +/* _dl_alloc.c */ +extern struct _dl_handle* _dl_root_handle; +extern struct _dl_handle* _dl_top_handle; +extern struct _dl_handle* _dl_free_list; +#ifndef __DIET_LD_SO__ +void _dl_free_handle(struct _dl_handle* dh); +struct _dl_handle* _dl_get_handle(); +struct _dl_handle* _dl_find_lib(const char* name); + +/* _dl_open.c */ +void *_dl_open(const char* filename, int flags); + +/* _dl_load.c */ +void *_dl_load(const char* filename, const char*pathname, int fd, int flags); +struct _dl_handle* _dl_dyn_scan(struct _dl_handle* dh, void* dyn_addr, int flags); + +/* _dl_search.c */ +void _dl_set_rpath(const char *path); +const char* _dl_get_rpath(); +int _dl_search(char *buf, int len, const char *filename); + +/* dlsym.c */ +void *_dlsym(void*dh,char*symbol); +void *_dl_sym_search_str(struct _dl_handle*h,char*name); +void *_dl_sym(struct _dl_handle * h, int symbol); + +/* _dl_queue.c */ +int _dl_queue_lib(const char* name, int flags); +int _dl_open_dep(); + +/* _dl_relocate.c */ +int _dl_relocate(struct _dl_handle* dh, _dl_rel_t *rel, int num); + +/* dlerror.c */ +extern unsigned int _dl_error; +extern const char* _dl_error_location; +extern const char* _dl_error_data; +#endif #endif diff --git a/mdk-stage1/dietlibc/libdl/_dl_jump.S b/mdk-stage1/dietlibc/libdl/_dl_jump.S index 134f31ea5..bd7115258 100644 --- a/mdk-stage1/dietlibc/libdl/_dl_jump.S +++ b/mdk-stage1/dietlibc/libdl/_dl_jump.S @@ -1,8 +1,55 @@ .text .global _dl_jump _dl_jump: + +#ifdef __i386__ +.type _dl_jump,@function + + pushl %eax # save register args... + pushl %ecx + pushl %edx + + pushl %ebx + + pushl 20(%esp) # 2. arg from plt + pushl 20(%esp) # 1. arg from plt + +#ifdef __DYN_LIB + call 1f +1: popl %ebx + addl $_GLOBAL_OFFSET_TABLE_+[.-1b],%ebx + call do_rel@PLT +#else call do_rel - pop %edx - pop %edx - jmp *%eax +#endif + addl $8, %esp + + popl %ebx + + popl %edx # restore register args... + popl %ecx + + xchgl %eax, (%esp) # restore eax and save function pointer (for return) + + ret $8 # remove arguments from plt and jump to REAL function + +#endif + +#ifdef __arm__ +.type _dl_jump,function + + stmdb sp!, {r0, r1, r2, r3} + sub r1, ip, lr + sub r1, r1, #4 + add r1, r1, r1 /* dyntab entry */ + ldr r0, [lr, #-4] /* dynlib handle */ +#ifdef __DYN_LIB + bl do_rel(PLT) +#else + bl do_rel +#endif + mov r12, r0 + ldmia sp!, {r0, r1, r2, r3, lr} + mov pc, r12 +#endif diff --git a/mdk-stage1/dietlibc/libdl/_dl_open.c b/mdk-stage1/dietlibc/libdl/_dl_open.c index 1f907db21..f7bd0af95 100644 --- a/mdk-stage1/dietlibc/libdl/_dl_open.c +++ b/mdk-stage1/dietlibc/libdl/_dl_open.c @@ -1,207 +1,35 @@ -#include <stdio.h> -#include <unistd.h> -#include <sys/mman.h> +#include <fcntl.h> #include <dlfcn.h> -#include <linux/elf.h> +#include <limits.h> +#ifndef __DIET_LD_SO__ +#include <string.h> +#endif #include "_dl_int.h" -struct _dl_handle dl_test; - -#define _ELF_DWN_ROUND(ps,n) ((n)&(~((ps)-1))) -#define _ELF_UP_ROUND(ps,n) ((((n)&((ps)-1))?(ps):0)+ _ELF_DWN_ROUND((ps),(n))) -#define _ELF_RST_ROUND(ps,n) ((n)&((ps)-1)) - -void _dl_jump(); - -/* - * this file is a Q. & D. hack ... don't think this is bug free or meaningfull - */ - -static void *do_map_in(void *base, unsigned long length, int flags, int fd, unsigned long offset) -{ - int perm = 0; - if (flags & PF_X) perm|=PROT_EXEC; - if (flags & PF_R) perm|=PROT_READ; - if (flags & PF_W) perm|=PROT_WRITE; - return mmap(base, length, perm, MAP_PRIVATE|((base)?MAP_FIXED:0), fd, offset); -} - -unsigned long do_rel(struct _dl_handle * tmp_dl, unsigned long off) -{ -// struct _dl_handle * tmp_dl = ((void*)*((&off)-1)); - Elf32_Rel *tmp = ((void*)tmp_dl->plt_rel)+off; - int sym=ELF32_R_SYM(tmp->r_info); - register unsigned long sym_val; - - printf("do_rel %08x %08x\n",tmp_dl,off); - - printf ("do_rel %08x+%x\n",tmp_dl->plt_rel,off); - printf("do_rel @ %08x with type %d -> %d\n",tmp->r_offset,ELF32_R_TYPE(tmp->r_info),sym); - - printf("do_rel sym %08x\n",tmp_dl->dyn_sym_tab[sym].st_value); - - /* modify GOT for REAL symbol */ - sym_val=((unsigned long)(tmp_dl->mem_base+tmp_dl->dyn_sym_tab[sym].st_value)); - *((unsigned long*)(tmp_dl->mem_base+tmp->r_offset))=sym_val; - - printf("do_rel sym %08x\n",sym_val); - /* HOWTO JUMP ?!? */ - return sym_val; -} - -void *_dl_open(const char*pathname, int fd, int flag) -{ - int ps=getpagesize(); - int i; - unsigned char buf[1024]; - char *m=0,*d; - struct _dl_handle *ret=0; - - unsigned long l; - - Elf32_Ehdr *eh; - Elf32_Phdr *ph; - - int ld_nr=0; - Elf32_Phdr *ld[4]={0,0,0,0}; - Elf32_Phdr *dyn=0; - - if (fd==-1) return 0; - - printf("_dl_open: %s\n",pathname); - - read(fd, buf, 1024); - eh=(Elf32_Ehdr*)buf; - ph=(Elf32_Phdr*)&buf[eh->e_phoff]; - - for (i=0; i<eh->e_phnum; i++) { - if (ph[i].p_type==PT_LOAD) { - ld[ld_nr++]=ph+i; - } - if (ph[i].p_type==PT_DYNAMIC) { - dyn=ph+i; - } +#ifdef __DIET_LD_SO__ +static +#endif +void*_dl_open(const char*filename,int flags) { + int fd; + char buf[PATH_MAX]; + const char*p=0; + + for (fd=0;filename[fd] && (p==0);++fd) if (filename[fd]=='/') p=filename; + if (p) { +#ifdef __DIET_LD_SO__ + if ((fd=_dl_sys_open(p,O_RDONLY,0))<0) fd=-1; +#else + fd=open(p,O_RDONLY); +#endif + } else { + p=buf; + fd=_dl_search(buf,sizeof(buf)-1,filename); } - - if (ld_nr==1) { - unsigned long offset = _ELF_DWN_ROUND(ps,ld[0]->p_offset); - unsigned long off = _ELF_RST_ROUND(ps,ld[0]->p_offset); - unsigned long length = _ELF_UP_ROUND(ps,ld[0]->p_memsz+off); - m = (char*)do_map_in(0, length, ld[0]->p_flags, fd, offset); - - /* zero pad bss */ - l = ld[0]->p_offset+ld[0]->p_filesz; - memset(m+l,0,length-l); - - dl_test.mem_base=m; - dl_test.mem_size=length; - dl_test.lnk_count=0; - - ret = &dl_test; - } - else if (ld_nr==2) { /* aem... yes Quick & Really Dirty / for the avarage 99% */ - //unsigned long text_addr = _ELF_DWN_ROUND(ps,ld[0]->p_vaddr); - unsigned long text_offset = _ELF_DWN_ROUND(ps,ld[0]->p_offset); - unsigned long text_off = _ELF_RST_ROUND(ps,ld[0]->p_offset); - unsigned long text_size = _ELF_UP_ROUND(ps,ld[0]->p_memsz+text_off); - - unsigned long data_addr = _ELF_DWN_ROUND(ps,ld[1]->p_vaddr); - unsigned long data_offset = _ELF_DWN_ROUND(ps,ld[1]->p_offset); - unsigned long data_off = _ELF_RST_ROUND(ps,ld[1]->p_offset); - unsigned long data_size = _ELF_UP_ROUND(ps,ld[1]->p_memsz+data_off); - unsigned long data_fsize = _ELF_UP_ROUND(ps,ld[1]->p_filesz+data_off); - - /* mmap all mem_blocks for *.so */ - l = text_size+data_size; - - dl_test.mem_size=l; - - m = (char*) do_map_in(0,l,ld[0]->p_flags,fd,text_offset); - - /* release data,bss part */ - mprotect(m+data_addr, data_size, PROT_NONE); - - /* mmap data,bss part */ - d = (char*) do_map_in(m+data_addr,data_fsize,ld[1]->p_flags,fd,data_offset); - - /* zero pad bss */ - l = data_off+ld[1]->p_filesz; - memset(d+l,0,data_size-l); - - /* more bss ? */ - if (data_size>data_fsize) { - l = data_size-data_fsize; - mmap(d+data_fsize, l, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0); - } - - dl_test.mem_base=m; - dl_test.lnk_count=0; - ret = &dl_test; - } - - printf("_dl_open pre resolv\n"); - if (ret) { - Elf32_Dyn* dyn_tab = (void*)m+dyn->p_vaddr; - void (*init)(); - unsigned long* got=0; - void* jmprel=0; - int pltreltype=0; - int pltrelsize=0; - - printf("_dl_open IN resolv\n"); - for(i=0;dyn_tab[i].d_tag;i++) { - if (dyn_tab[i].d_tag==DT_HASH) { - ret->hash_tab = (unsigned long*)(m+dyn_tab[i].d_un.d_ptr); - } - if (dyn_tab[i].d_tag==DT_SYMTAB) { - ret->dyn_sym_tab = (Elf32_Sym*)(m+dyn_tab[i].d_un.d_ptr); - } - if (dyn_tab[i].d_tag==DT_STRTAB) { - ret->dyn_str_tab = (char*)(m+dyn_tab[i].d_un.d_ptr); - } - if (dyn_tab[i].d_tag==DT_FINI) { - ret->fini = (void(*)(void))(m+dyn_tab[i].d_un.d_val); - } - if (dyn_tab[i].d_tag==DT_INIT) { - init = (void(*)(void))(m+dyn_tab[i].d_un.d_val); - printf("init @ %08x\n",init); - } - if (dyn_tab[i].d_tag==DT_PLTGOT) { - got=(unsigned long*)(m+dyn_tab[i].d_un.d_val); - } - if (dyn_tab[i].d_tag==DT_PLTREL) { - pltreltype=dyn_tab[i].d_un.d_val; - } - if (dyn_tab[i].d_tag==DT_PLTRELSZ) { - pltrelsize=dyn_tab[i].d_un.d_val; - } - if (dyn_tab[i].d_tag==DT_JMPREL) { - jmprel=(m+dyn_tab[i].d_un.d_val); - dl_test.plt_rel=jmprel; - } - } - /* GOT */ - got[0]+=(unsigned long)m; - got[1]=(unsigned long)&dl_test; -// got[2]=(unsigned long)do_rel; - got[2]=(unsigned long)(_dl_jump); - /* */ - - if (pltreltype == DT_REL) { - Elf32_Rel *tmp = jmprel; - for (;(char*)tmp<(((char*)jmprel)+pltrelsize);(char*)tmp=((char*)tmp)+sizeof(Elf32_Rel)) { - *((unsigned long*)(m+tmp->r_offset))+=(unsigned long)m; -// *((unsigned long*)(m+tmp->r_offset))+=(unsigned long)do_rel; - printf("rel @ %08x with type %d -> %d\n",tmp->r_offset,ELF32_R_TYPE(tmp->r_info),ELF32_R_SYM(tmp->r_info)); - } - } - - printf("_dl_open post resolv, pre init\n"); - init(); + if (fd==-1) { + _dl_error_data=filename; + _dl_error=1; + return 0; } - printf("_dl_open post resolv, init\n"); - - close(fd); - return ret; + return _dl_load(filename,p,fd,flags); } diff --git a/mdk-stage1/dietlibc/libdl/_dl_search.c b/mdk-stage1/dietlibc/libdl/_dl_search.c index 5e3217d46..ccf9c51c5 100644 --- a/mdk-stage1/dietlibc/libdl/_dl_search.c +++ b/mdk-stage1/dietlibc/libdl/_dl_search.c @@ -1,68 +1,95 @@ -#include <unistd.h> #include <stdlib.h> -#include <string.h> #include <dlfcn.h> #include <fcntl.h> -#include <stdio.h> + +#include "_dl_int.h" #define WANT_LD_SO_CONF_SEARCH static const char *_dl_search_rpath=0; + +#ifndef __DIET_LD_SO__ +#include <unistd.h> +#include <string.h> void _dl_set_rpath(const char *path) { _dl_search_rpath=path; } +const char* _dl_get_rpath() { return _dl_search_rpath; } +#endif /* search a colon (semicolon) seperated path for the libraray "filename" */ -static int _dl_search_path(char *buf, int len, const char*path, const int pathlen, const char *filename) -{ - int fd,l=len,i=1; - const char *c,*pe=path+pathlen; +static int _dl_search_path(char*buf,int len,const char*path,const int pathlen,const char*filename) { + int fd,i=1,fl=strlen(filename),ml=len-fl; + const char*c,*pe=path+pathlen; if (path) { - for (c=path; c<pe; c+=i) { + for (c=path;c<pe;c+=i) { + int l=len-1; if ((*c==':')||(*c==';')) ++c; i=strcspn(c,":;"); if (i) { - strncpy(buf, c, i); buf[i]=0; - l-=i; - strncat(buf, "/", l); + if (i>ml) continue; /* if len(path-entry)+len(filename)+2 is greater than the buffer ? SKIP */ + memcpy(buf,c,i); + buf[i]='/'; + l-=++i; } - else - buf[0]=0; - strncat(buf, filename, --l); -// printf("_dl_search: %s\n",buf); + memcpy(buf+i,filename,fl); + buf[i+fl]=0; +#ifdef DEBUG +// pf(__func__": "); pf(buf); pf("\n"); +#endif +#ifdef __DIET_LD_SO__ + if ((fd=_dl_sys_open(buf,O_RDONLY,0))>-1) return fd; +#else if ((fd=open(buf,O_RDONLY))!=-1) return fd; +#endif } } return -1; } /* parse the SMALL file "conf" for lib directories (aem... hang me if you can :) ) */ -static int _dl_search_conf(char *buf, int len, const char *conf, const char *filename) -{ +static int _dl_search_conf(char*buf,int len,const char*conf,const char*filename) { char ld_so_conf[1024]; - int i,l,fd=open(conf,O_RDONLY); - if (fd!=-1) { + int i,l,fd; +#ifdef __DIET_LD_SO__ + if ((fd=_dl_sys_open(conf,O_RDONLY,0))>-1) { + l=_dl_sys_read(fd,ld_so_conf,sizeof(ld_so_conf)-1); +#else + if ((fd=open(conf,O_RDONLY))!=-1) { l=read(fd,ld_so_conf,sizeof(ld_so_conf)-1); +#endif ld_so_conf[sizeof(ld_so_conf)-1]=0; +#ifdef __DIET_LD_SO__ + _dl_sys_close(fd); +#else close(fd); - if (ld_so_conf[l-1]=='\n') ld_so_conf[--l]=0; - for (i=0;i<l;i++) if (ld_so_conf[i]=='\n') ld_so_conf[i]=':'; - if ((fd=_dl_search_path(buf,len,ld_so_conf,l,filename))!=-1) return fd; +#endif + if (l>0) { + if (ld_so_conf[l-1]=='\n') ld_so_conf[--l]=0; + for (i=0;i<l;i++) if (ld_so_conf[i]=='\n') ld_so_conf[i]=':'; + if ((fd=_dl_search_path(buf,len,ld_so_conf,l,filename))!=-1) return fd; + } } return -1; } -int _dl_search(char *buf, int len, const char *filename) -{ +#ifdef __DIET_LD_SO__ +static +#endif +int _dl_search(char*buf,int len,const char*filename) { int fd; - /* 1. search the LD_RUN_PATH (from the executable */ + /* 1. search the LD_RUN_PATH (from the executable) */ if (_dl_search_rpath) { if ((fd=_dl_search_path(buf,len,_dl_search_rpath,strlen(_dl_search_rpath),filename))!=-1) return fd; } /* 2. IF we have a "secure" enviroment THEN search LD_LIBRARY_PATH */ - if (getuid()==geteuid()) { +#ifdef __DIET_LD_SO__ + if ((at_uid==at_euid)&&(at_gid==at_egid)) { +#else + if ((getuid()==geteuid())&&(getgid()==getegid())) { +#endif char *p=getenv("LD_LIBRARY_PATH"); if (p) if ((fd=_dl_search_path(buf,len,p,strlen(p),filename))!=-1) return fd; @@ -78,7 +105,7 @@ int _dl_search(char *buf, int len, const char *filename) /* default path search */ { - const char* def_path="/usr/lib:/lib"; + const char def_path[]="/usr/lib:/lib"; return _dl_search_path(buf,len,def_path,strlen(def_path),filename); } } diff --git a/mdk-stage1/dietlibc/libdl/dlclose.c b/mdk-stage1/dietlibc/libdl/dlclose.c index 384550c47..6fafedcf5 100644 --- a/mdk-stage1/dietlibc/libdl/dlclose.c +++ b/mdk-stage1/dietlibc/libdl/dlclose.c @@ -1,14 +1,38 @@ +#include <sys/mman.h> + #include "_dl_int.h" -int dlclose (void *handle) -{ - if (handle) { - struct _dl_handle *h = handle; - if (h->lnk_count) { - --h->lnk_count; - return -1; +static void dec_referenced_libs(struct _dl_handle*dh) { + Elf_Dyn* dyn_tab=dh->dynamic; + int i; + for(i=0;dyn_tab[i].d_tag;i++) { + if (dyn_tab[i].d_tag==DT_NEEDED) { + char *lib_name=dh->dyn_str_tab+dyn_tab[i].d_un.d_val; +#ifdef DEBUG + pf(__FUNCTION__); pf(": lib: "); pf(lib_name); pf("\n"); +#endif + dlclose(_dl_find_lib(lib_name)); } - if (munmap(h->mem_base,h->mem_size)!=0) return -1; + } +} + +int dlclose(void*handle) { + _dl_error_location="dlclose"; + if (handle) { + struct _dl_handle*dh=handle; + if (--(dh->lnk_count)) return 0; /* not yet unreferenced */ + +#ifdef DEBUG + pf(__FUNCTION__); pf(": "); pf(dh->name); pf("\n"); +#endif + if (dh->fini) dh->fini(); + dec_referenced_libs(dh); +#ifdef __DIET_LD_SO__ + if (_dl_sys_munmap(dh->mem_base,dh->mem_size)<0) return -1; +#else + if (munmap(dh->mem_base,dh->mem_size)==-1) return -1; +#endif + _dl_free_handle(handle); } return 0; } diff --git a/mdk-stage1/dietlibc/libdl/dlerror.c b/mdk-stage1/dietlibc/libdl/dlerror.c index 1e31bae54..3fea0b75c 100644 --- a/mdk-stage1/dietlibc/libdl/dlerror.c +++ b/mdk-stage1/dietlibc/libdl/dlerror.c @@ -1,4 +1,55 @@ -const char *dlerror(void) -{ - return "HAE ?!?"; + +#include "_dl_int.h" + +#ifdef __DIET_LD_SO__ +static unsigned int _dl_error; +static const char*_dl_error_location; +static const char*_dl_error_data; +#else +#include <string.h> +unsigned int _dl_error; +const char*_dl_error_location; +const char*_dl_error_data; +#endif + +static struct _dl_err_msg { + char*msg; + int len; +} _dl_error_msg[]={ +#define MSG(n) { (n), sizeof((n))-1 } + MSG("can't open: "), /* 1 */ + MSG("can't stat: "), /* 2 */ + MSG("shared object is not position independent: "), /* 3 */ + MSG("can't resolve all symbols in: "), /* 4 */ + MSG("can't find symbol: "), /* 5 */ + MSG("invalid relocation type in: "), /* 6 */ + MSG("internal error: layout not yet supported: "), /* 7 */ +}; + +const char *dlerror(void) { + static char buf[1024],*p=buf; + register int l,len=sizeof(buf)-1; + if (_dl_error==0) return 0; + + buf[0]=0; + buf[len]=0; + --_dl_error; + + if (_dl_error>=(sizeof(_dl_error_msg)/sizeof(struct _dl_err_msg))) + return "HAE ?!?"; + + if (_dl_error_location) { + l=strlen(_dl_error_location); + strncpy(p,_dl_error_location,len); len-=l; p+=l; + strncpy(p,": ",len); len-=2; p+=2; + } + l=_dl_error_msg[_dl_error].len; + strncpy(p,_dl_error_msg[_dl_error].msg,len); len-=l; p+=l; + strncpy(p,_dl_error_data,len); + + _dl_error_location=0; + _dl_error_data=""; + _dl_error=0; + + return buf; } diff --git a/mdk-stage1/dietlibc/libdl/dlopen.c b/mdk-stage1/dietlibc/libdl/dlopen.c index 1693d889f..79fa7903c 100644 --- a/mdk-stage1/dietlibc/libdl/dlopen.c +++ b/mdk-stage1/dietlibc/libdl/dlopen.c @@ -2,20 +2,25 @@ #include <dlfcn.h> #include <limits.h> -int _dl_search(char*buf, int bufsize, const char*filename); -void *_dl_open(const char*pathname, int fd, int flag); +#include "_dl_int.h" -void *dlopen (const char *filename, int flag) -{ - int fd; - char buf[PATH_MAX]; - const char *p; - if (*filename=='/') - fd=open(p=filename,O_RDONLY); - else { - p=buf; - fd=_dl_search(buf,sizeof(buf),filename); +#ifdef __DIET_LD_SO__ +static +#endif +void*_dlopen(const char *filename, int flags) { + struct _dl_handle* ret; + if (filename) { + if ((ret=_dl_find_lib(filename))) { + ++(ret->lnk_count); /* add a reference */ + return ret; + } + return _dl_open(filename,flags); } + /* return 1 as an indicator for dlsym to search ALL global objects */ + return RTLD_DEFAULT; +} - return _dl_open(p,fd,flag); +void*dlopen(const char *filename, int flags) { + _dl_error_location="dlopen"; + return _dlopen(filename,flags|RTLD_USER|RTLD_NOSONAME); } diff --git a/mdk-stage1/dietlibc/libdl/dlsym.c b/mdk-stage1/dietlibc/libdl/dlsym.c index 2af940837..c58bb4d11 100644 --- a/mdk-stage1/dietlibc/libdl/dlsym.c +++ b/mdk-stage1/dietlibc/libdl/dlsym.c @@ -1,7 +1,92 @@ #include "_dl_int.h" -void *dlsym(void *handle, char *symbol) -{ - printf("dlsym(%08x,%s) -> %08x\n",handle,symbol,elf_hash(symbol)%17); - return 0; +#include "elf_hash.h" + +#ifdef __DIET_LD_SO__ +static +#endif +void *_dlsym(void*handle,char*symbol) { + unsigned long*sym=0; + if (handle) { + struct _dl_handle*dh=(struct _dl_handle*)handle; + unsigned long hash =elf_hash(symbol); + unsigned long bhash=hash%HASH_BUCKET_LEN(dh->hash_tab); + unsigned long*chain=HASH_CHAIN(dh->hash_tab); + unsigned long ind; + char *name=dh->dyn_str_tab; + +#ifdef DEBUG +// pf(__FUNCTION__); pf(": bucket("); ph(bhash); pf(",\""); pf(symbol); pf("\")\n"); +#endif + + ind=HASH_BUCKET(dh->hash_tab)[bhash]; +#ifdef DEBUG +// pf(__FUNCTION__); pf(": chain ("); ph(ind); pf(",\""); pf(symbol); pf("\")\n"); +#endif + + while(ind) { + int ptr=dh->dyn_sym_tab[ind].st_name; +#ifdef DEBUG +// pf(__FUNCTION__); pf(": symbol(\""); pf(name+ptr); pf("\",\"); pf(symbol); pf("\")\n"); +#endif + if (strcmp(name+ptr,symbol)==0) { + if (ELF_ST_TYPE(dh->dyn_sym_tab[ind].st_shndx)!=0) { + sym=(long*)(dh->mem_base+dh->dyn_sym_tab[ind].st_value); + break; /* ok found ... */ + } + } + ind=chain[ind]; + } +#ifdef DEBUG + pf(__FUNCTION__); pf(": symbol \""); pf(symbol); pf("\" @ "); ph((long)sym); pf("\n"); +#endif + } + return sym; +} + +#ifdef __DIET_LD_SO__ +static +#endif +void*_dl_sym_search_str(struct _dl_handle*dh,char*name) { + void *sym=0; + struct _dl_handle*tmp; +#ifdef DEBUG + pf(__FUNCTION__); pf(": search for: "); pf(name); pf("\n"); +#endif + for (tmp=_dl_root_handle;tmp && (!sym);tmp=tmp->next) { +// if (!(tmp->flags&RTLD_GLOBAL)) continue; +#ifdef DEBUG + pf(__FUNCTION__); pf(": searching in "); pf(tmp->name); pf("\n"); +#endif + sym=_dlsym((void*)tmp,name); +#ifdef DEBUG + if (sym) { pf(__FUNCTION__); pf(": found: "); pf(name); pf(" @ "); ph((long)sym); pf("\n"); } +#endif + } + return sym; +} + +#ifdef __DIET_LD_SO__ +static +#endif +void*_dl_sym(struct _dl_handle*dh,int symbol) { + char *name=dh->dyn_str_tab+dh->dyn_sym_tab[symbol].st_name; + void*sym=_dl_sym_search_str(dh,name); +#ifdef DEBUG + pf(__FUNCTION__); pf(": "); ph(symbol); pf(" -> "); ph((long)sym); pf("\n"); +#endif + return sym; +} + +void*dlsym(void*handle,char*symbol) { + void*h; + if (handle==RTLD_DEFAULT || !handle /* RTLD_DEFAULT is NULL on glibc */ ) + h=_dl_sym_search_str(0,symbol); + else h=_dlsym(handle,symbol); + if (h==0) { + _dl_error_location="dlsym"; + _dl_error_data=symbol; + _dl_error=5; + } + return h; } |