diff --git a/src/bin/Makefile b/src/bin/Makefile index 4593701..e279384 100644 --- a/src/bin/Makefile +++ b/src/bin/Makefile @@ -1,8 +1,8 @@ # $Id$ # The System Makefile (C) 2002 The UbixOS Project -all: init-bin login-bin shell-bin ls-bin clock-bin cp-bin fdisk-bin format-bin disklabel-bin ubistry-bin edit-bin -# test-bin pwd-bin cat-bin ld-dyn-bin de-bin goofball-bin +all: init-bin login-bin shell-bin ls-bin clock-bin cp-bin fdisk-bin format-bin disklabel-bin ubistry-bin edit-bin ld-bin +# test-bin pwd-bin cat-bin de-bin goofball-bin init-bin: init (cd init;make) @@ -25,8 +25,8 @@ cat-bin: cat (cd cat;make) -ld-dyn-bin: ld-dyn - (cd ld-dyn;make) +ld-bin: ld + (cd ld;make) de-bin: de (cd de;make) diff --git a/src/bin/cp/Makefile b/src/bin/cp/Makefile index 8cf5d52..8f6dc57 100644 --- a/src/bin/cp/Makefile +++ b/src/bin/cp/Makefile @@ -20,6 +20,8 @@ #Startup File STARTUP = ../../lib/ubix/startup.o +LIBRARIES = ../../lib/libc/libc.so + # Link The Binary $(BINARY) : $(OBJS) $(CC) $(CFLAGS) -o $@ $(STARTUP) $(LIBRARIES) $(OBJS) diff --git a/src/bin/disklabel/Makefile b/src/bin/disklabel/Makefile index 079567c..41d3422 100644 --- a/src/bin/disklabel/Makefile +++ b/src/bin/disklabel/Makefile @@ -20,6 +20,9 @@ #Startup File STARTUP = ../../lib/ubix/startup.o + +LIBRARIES = ../../lib/libc/libc.so + # Link The Binary $(BINARY) : $(OBJS) $(CC) $(CFLAGS) -o $@ $(STARTUP) $(LIBRARIES) $(OBJS) diff --git a/src/bin/edit/Makefile b/src/bin/edit/Makefile index 87eaa45..319a799 100644 --- a/src/bin/edit/Makefile +++ b/src/bin/edit/Makefile @@ -20,6 +20,8 @@ #Startup File STARTUP = ../../lib/ubix/startup.o +LIBRARIES = ../../lib/libc/libc.so + # Link The Binary $(BINARY) : $(OBJS) $(CC) $(CFLAGS) -o $@ $(STARTUP) $(LIBRARIES) $(OBJS) diff --git a/src/bin/fdisk/Makefile b/src/bin/fdisk/Makefile index 7203937..4a890bc 100644 --- a/src/bin/fdisk/Makefile +++ b/src/bin/fdisk/Makefile @@ -20,6 +20,8 @@ #Startup File STARTUP = ../../lib/ubix/startup.o +LIBRARIES = ../../lib/libc/libc.so + # Link The Binary $(BINARY) : $(OBJS) $(CC) $(CFLAGS) -o $@ $(STARTUP) $(LIBRARIES) $(OBJS) diff --git a/src/bin/format/Makefile b/src/bin/format/Makefile index 0fcf67e..d7e4329 100644 --- a/src/bin/format/Makefile +++ b/src/bin/format/Makefile @@ -20,6 +20,8 @@ #Startup File STARTUP = ../../lib/ubix/startup.o +LIBRARIIES = ../../lib/libc/libc.so + # Link The Binary $(BINARY) : $(OBJS) $(CC) $(CFLAGS) -o $@ $(STARTUP) $(LIBRARIES) $(OBJS) diff --git a/src/bin/ld/main.c b/src/bin/ld/main.c index 841b7aa..4b88fb4 100644 --- a/src/bin/ld/main.c +++ b/src/bin/ld/main.c @@ -24,6 +24,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. $Log$ + Revision 1.3 2004/06/16 13:52:51 reddawg + Start of userland LD + Revision 1.2 2004/06/01 01:30:43 reddawg No more warnings and organized make files @@ -43,15 +46,221 @@ #include #include -#include -#include -#include +#include +#include "elf.h" +void _ld(); +asm( + ".globl _ld \n" + "_ld: \n" + " call ld \n" + " pop %ecx \n" + " pop %ecx \n" + " push %eax \n" + " ret \n" + ); + +uInt32 ldFindFunc(char *func); + +elfHeader *binaryHeader = 0x0; +elfSectionHeader *binarySectionHeader = 0x0; +char *binaryShStr = 0x0; +char *binaryDynStr = 0x0; +elfDynSym *binaryRelSymTab = 0x0; +elfPltInfo *binaryElfRel = 0x0; + +elfHeader *linkerHeader = 0x0; +elfSectionHeader *linkerSectionHeader = 0x0; +elfProgramHeader *linkerProgramHeader = 0x0; +elfDynSym *linkerRelSymTab = 0x0; +elfPltInfo *linkerElfRel = 0x0; +char *linkerShStr = 0x0; +char *linkerDynStr = 0x0; + +char *output = 0x0; +int sym = 0x0; -int main(int argc,char **argv) { - printf("test\n"); - while (1); - return(0); +uInt32 ld(uInt32 got2,uInt32 entry) { + int i = 0x0; + int x = 0x0; + int rel = 0x0; + FILE *binaryFd = 0x0; + + binaryFd = malloc(sizeof(FILE)); + binaryFd->fd = got2; + + if (binaryHeader == 0x0) { + fseek(binaryFd,0x0,0x0); + binaryHeader = (elfHeader *)malloc(sizeof(elfHeader)); + fread(binaryHeader,sizeof(elfHeader),1,binaryFd); + } + + if (binarySectionHeader == 0x0) { + binarySectionHeader = (elfSectionHeader *)malloc(sizeof(elfSectionHeader)*binaryHeader->eShnum); + fseek(binaryFd,binaryHeader->eShoff,0); + fread(binarySectionHeader,sizeof(elfSectionHeader),binaryHeader->eShnum,binaryFd); + + if (binaryShStr == 0x0) { + binaryShStr = (char *)malloc(binarySectionHeader[binaryHeader->eShstrndx].shSize); + fseek(binaryFd,binarySectionHeader[binaryHeader->eShstrndx].shOffset,0); + fread(binaryShStr,binarySectionHeader[binaryHeader->eShstrndx].shSize,1,binaryFd); + } + + for (i=0x0;ieShnum;i++) { + switch (binarySectionHeader[i].shType) { + case 3: + if (!strcmp((binaryShStr + binarySectionHeader[i].shName),".dynstr")) { + if (binaryDynStr == 0x0) { + binaryDynStr = (char *)malloc(binarySectionHeader[i].shSize); + fseek(binaryFd,binarySectionHeader[i].shOffset,0); + fread(binaryDynStr,binarySectionHeader[i].shSize,1,binaryFd); + } + } + break; + case 9: + rel = i; + break; + case 11: + if (binaryRelSymTab == 0x0) { + binaryRelSymTab = (elfDynSym *)malloc(binarySectionHeader[i].shSize); + fseek(binaryFd,binarySectionHeader[i].shOffset,0); + fread(binaryRelSymTab,binarySectionHeader[i].shSize,1,binaryFd); + } + break; + } + } + } + + if (binaryElfRel == 0x0) { + fseek(binaryFd,binarySectionHeader[rel].shOffset,0x0); + binaryElfRel = (elfPltInfo *)malloc(binarySectionHeader[rel].shSize); + fread(binaryElfRel,binarySectionHeader[rel].shSize,1,binaryFd); + } + + + i = (entry/sizeof(elfPltInfo)); + x = ELF32_R_SYM(binaryElfRel[i].pltInfo); + //printf("Find Function [%s]\n",binaryDynStr + binaryRelSymTab[x].dynName); + i = ldFindFunc(binaryDynStr + binaryRelSymTab[x].dynName); + free(binaryFd); + + return(i); + } + +uInt32 ldFindFunc(char *func) { + int i = 0x0; + int x = 0x0; + int rel = 0x0; + uInt32 *funcPtr = 0x0; + uInt32 *reMap = 0x0; + char *newLoc = 0x0; + FILE *linkerFd = 0x0; + + if (output == 0x0) { + linkerFd = fopen("libc.so","rb"); + output = (char *)malloc((linkerFd->size+0x4000)); + } + + if (linkerHeader == 0x0) { + fseek(linkerFd,0x0,0x0); + linkerHeader = (elfHeader *)malloc(sizeof(elfHeader)); + fread(linkerHeader,sizeof(elfHeader),1,linkerFd); + } + + if (linkerProgramHeader == 0x0) { + linkerProgramHeader = (elfProgramHeader *)malloc(sizeof(elfProgramHeader)*linkerHeader->ePhnum); + fseek(linkerFd,linkerHeader->ePhoff,0); + fread(linkerProgramHeader,sizeof(elfProgramHeader),linkerHeader->ePhnum,linkerFd); + + for (i=0;iePhnum;i++) { + switch (linkerProgramHeader[i].phType) { + case 1: + case 2: + newLoc = (char *)linkerProgramHeader[i].phVaddr + (uInt32)output; + fseek(linkerFd,linkerProgramHeader[i].phOffset,0); + fread(newLoc,linkerProgramHeader[i].phFilesz,1,linkerFd); + break; + default: + printf("Unhandled Header\n"); + break; + } + } + } + + if (linkerSectionHeader == 0x0) { + linkerSectionHeader = (elfSectionHeader *)malloc(sizeof(elfSectionHeader)*linkerHeader->eShnum); + fseek(linkerFd,linkerHeader->eShoff,0); + fread(linkerSectionHeader,sizeof(elfSectionHeader),linkerHeader->eShnum,linkerFd); + + if (linkerShStr == 0x0) { + linkerShStr = (char *)malloc(linkerSectionHeader[linkerHeader->eShstrndx].shSize); + fseek(linkerFd,linkerSectionHeader[linkerHeader->eShstrndx].shOffset,0); + fread(linkerShStr,linkerSectionHeader[linkerHeader->eShstrndx].shSize,1,linkerFd); + } + + for (i=0x0;ieShnum;i++) { + switch (linkerSectionHeader[i].shType) { + case 3: + if (!strcmp((linkerShStr + linkerSectionHeader[i].shName),".dynstr")) { + if (linkerDynStr == 0x0) { + linkerDynStr = (char *)malloc(linkerSectionHeader[i].shSize); + fseek(linkerFd,linkerSectionHeader[i].shOffset,0); + fread(linkerDynStr,linkerSectionHeader[i].shSize,1,linkerFd); + } + } + break; + case 9: + rel = i; + break; + case 11: + if (linkerRelSymTab == 0x0) { + linkerRelSymTab = (elfDynSym *)malloc(linkerSectionHeader[i].shSize); + fseek(linkerFd,linkerSectionHeader[i].shOffset,0); + fread(linkerRelSymTab,linkerSectionHeader[i].shSize,1,linkerFd); + sym = i; + } + break; + } + } + } + + if (linkerElfRel == 0x0) { + linkerElfRel = (elfPltInfo *)malloc(linkerSectionHeader[rel].shSize); + fseek(linkerFd,linkerSectionHeader[rel].shOffset,0x0); + fread(linkerElfRel,linkerSectionHeader[rel].shSize,1,linkerFd); + + for (i=0x0;i +#define LD_START 0x500000 + void _ld(); uInt32 ld(uInt32,uInt32); uInt32 ldFindFunc(char *); -void ldEnable(); +uInt32 ldEnable(uInt32); #endif diff --git a/src/sys/kernel/exec.c b/src/sys/kernel/exec.c index 4646775..110048b 100644 --- a/src/sys/kernel/exec.c +++ b/src/sys/kernel/exec.c @@ -331,6 +331,7 @@ int x = 0x0; uInt32 eStart = 0x0; uInt32 *tmp = 0x0; + uInt32 ldAddr = 0x0; char *newLoc = 0x0; char *linker = 0x0; @@ -412,7 +413,7 @@ fread(newLoc,programHeader[i].phFilesz,1,tmpFd); } else if (programHeader[i].phType == 3) { - ldEnable(tmpFd); + ldAddr = ldEnable(tmpFd); /* linker = (char *)kmalloc(programHeader[i].phMemsz); fseek(tmpFd,programHeader[i].phOffset,0); @@ -427,7 +428,7 @@ for (i=0;i<12;i++) { if (elfDynamicS[i].dynVal == 0x3) { tmp = (uInt32 *)elfDynamicS[i].dynPtr; - tmp[2] = (uInt32) &_ld; + tmp[2] = ldAddr; tmp[1] = (uInt32)tmpFd;//0xDEADBEEF;//elfDynamicS[i].dynPtr; // kprintf("[0x%X][0x%X]\n",argv,argc); } diff --git a/src/sys/kernel/file.c b/src/sys/kernel/file.c index 6ba330c..b6b680f 100644 --- a/src/sys/kernel/file.c +++ b/src/sys/kernel/file.c @@ -254,7 +254,6 @@ } void sysFseek(userFileDescriptor *userFd,long offset,int whence) { - kprintf("sysFseek: [0x%X][0x%X]\n",userFd,userFd->fd); userFd->fd->offset = offset+whence; } @@ -294,6 +293,9 @@ /*** $Log$ + Revision 1.2 2004/05/15 02:30:28 reddawg + Lots of changes + END ***/ diff --git a/src/sys/kernel/ld.c b/src/sys/kernel/ld.c index 73bcff4..2fcc11c 100644 --- a/src/sys/kernel/ld.c +++ b/src/sys/kernel/ld.c @@ -34,13 +34,24 @@ #include #include #include +#include #include -void ldEnable(uInt32 tmpFd) { +uInt32 ldEnable(uInt32 tmpFd) { + int i = 0x0; + int x = 0x0; + int rel = 0x0; + int sym = 0x0; + char *newLoc = 0x0; + char *shStr = 0x0; + char *dynStr = 0x0; + uInt32 *reMap = 0x0; fileDescriptor *ldFd = 0x0; elfHeader *binaryHeader = 0x0; elfProgramHeader *programHeader = 0x0; elfSectionHeader *sectionHeader = 0x0; + elfDynSym *relSymTab = 0x0; + elfPltInfo *elfRel = 0x0; ldFd = fopen("ld.so","rb"); @@ -52,11 +63,30 @@ fseek(ldFd,binaryHeader->ePhoff,0); fread(programHeader,sizeof(elfSectionHeader),binaryHeader->ePhnum,ldFd); + sectionHeader = (elfSectionHeader *)kmalloc(sizeof(elfSectionHeader)*binaryHeader->eShnum); + fseek(ldFd,binaryHeader->eShoff,0); + fread(sectionHeader,sizeof(elfSectionHeader),binaryHeader->eShnum,ldFd); + + shStr = (char *)kmalloc(sectionHeader[binaryHeader->eShstrndx].shSize); + fseek(ldFd,sectionHeader[binaryHeader->eShstrndx].shOffset,0); + fread(shStr,sectionHeader[binaryHeader->eShstrndx].shSize,1,ldFd); + + for (i=0;iePhnum;i++) { switch (programHeader[i].phType) { case 1: case 2: - kmemcpy((output+programHeader[i].phVaddr),(binary + programHeader[i].phOffset),programHeader[i].phFilesz); + newLoc = (char *)programHeader[i].phVaddr + LD_START; + /* + Allocate Memory Im Going To Have To Make This Load Memory With Correct + Settings so it helps us in the future + */ + for (x=0;x<=((programHeader[i].phMemsz)+4095);x+=0x1000) { + vmmRemapPage(vmmFindFreePage(_current->id),((programHeader[i].phVaddr & 0xFFFFF000) + x + LD_START)); + } + /* Now Load Section To Memory */ + fseek(ldFd,programHeader[i].phOffset,0); + fread(newLoc,programHeader[i].phFilesz,1,ldFd); break; default: kprintf("Unhandled Header\n"); @@ -64,7 +94,65 @@ } } + for (i=0x0;ieShnum;i++) { + switch (sectionHeader[i].shType) { + case 3: + if (!kstrcmp((shStr + sectionHeader[i].shName),".dynstr")) { + dynStr = (char *)kmalloc(sectionHeader[i].shSize); + fseek(ldFd,sectionHeader[i].shOffset,0x0); + fread(dynStr,sectionHeader[i].shSize,1,ldFd); + } + break; + case 9: + rel = i; + break; + case 11: + //kprintf("Dynamic Sym\n"); + relSymTab = (elfDynSym *)kmalloc(sectionHeader[i].shSize); + fseek(ldFd,sectionHeader[i].shOffset,0x0); + fread(relSymTab,sectionHeader[i].shSize,1,ldFd); + sym = i; + break; + } + } + + elfRel = (elfPltInfo *)kmalloc(sectionHeader[rel].shSize); + fseek(ldFd,sectionHeader[rel].shOffset,0x0); + fread(elfRel,sectionHeader[rel].shSize,1,ldFd); + + for (i=0x0;ieEntry + LD_START; + //kprintf("ld: [0x%X]\n",i); + fclose(ldFd); + kfree(dynStr); + kfree(shStr); + kfree(relSymTab); + kfree(elfRel); + kfree(sectionHeader); + kfree(programHeader); + kfree(binaryHeader); + return(i); } void _ld(); diff --git a/src/tools/Makefile b/src/tools/Makefile index d5b4d18..87d03e4 100644 --- a/src/tools/Makefile +++ b/src/tools/Makefile @@ -49,7 +49,8 @@ #(cp ../bin/format/format ./) (cp ../bin/ubistry/ubistry ./) (cp ../sys/boot/mbr/mbr ./) - (./format 50 2000 /dev/fd0 ubix.elf 0 login 3754 ROM8X14.DPF 3754 init 3754 ubistry 3754 shell 3754 userdb 3754 ls 3754 motd 3754 fdisk 3754 mbr 3754 cp 3754 clock 3754 libc.so 3754) + (cp ../bin/ld/ld.so ./) + (./format 50 2000 /dev/fd0 ubix.elf 0 login 3754 ROM8X14.DPF 3754 init 3754 ubistry 3754 shell 3754 userdb 3754 ls 3754 motd 3754 fdisk 3754 mbr 3754 cp 3754 clock 3754 libc.so 3754 ld.so 3754) #(./format 263 204361 /dev/md1 ubix.elf 0 format 3754 fdisk 3754 ROM8X14.DPF 3754 init 3754 login 3754 shell 3754 userdb 3754 ls 3754 motd 3754 cp 3754) (./format 1064 2000 /dev/fd0 shell 3754 clock 3754) @@ -66,3 +67,4 @@ #(rm -fr format) (rm -fr mbr) (rm -fr ubistry) + (rm -fr ld.so)