Debugging a segfault in ELF loader (when loading CLIShell)
authorJohn Hodge <[email protected]>
Sat, 7 May 2011 13:17:03 +0000 (21:17 +0800)
committerJohn Hodge <[email protected]>
Sat, 7 May 2011 13:17:03 +0000 (21:17 +0800)
AcessNative/ld-acess_src/elf.c
AcessNative/ld-acess_src/request.c
AcessNative/ld-acess_src/syscalls.c

index b99f190..c01d24d 100644 (file)
@@ -2,7 +2,7 @@
  * Acess v0.1\r
  * ELF Executable Loader Code\r
  */\r
-#define DEBUG  0\r
+#define DEBUG  1\r
 #include <stdlib.h>\r
 #include <stdio.h>\r
 #include <string.h>\r
@@ -248,6 +248,9 @@ uintptr_t Elf_Relocate(void *Base)
 \r
        hdr->entrypoint += iBaseDiff;\r
        \r
+       hdr->misc.SymTable = 0;\r
+       hdr->misc.HashTable = 0;\r
+       \r
        // === Get Symbol table and String Table ===\r
        for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++)\r
        {\r
@@ -274,6 +277,9 @@ uintptr_t Elf_Relocate(void *Base)
                        break;\r
                }\r
        }\r
+       \r
+       LOG("hdr->misc.SymTable = %x, hdr->misc.HashTable = %x",\r
+               hdr->misc.SymTable, hdr->misc.HashTable);\r
 \r
 \r
        // Alter Symbols to true base\r
@@ -486,6 +492,9 @@ int Elf_GetSymbol(void *Base, char *Name, uintptr_t *ret)
        pBuckets = PTR(hdr->misc.HashTable);\r
        symtab = PTR(hdr->misc.SymTable);\r
        \r
+       if(!pBuckets || !symtab)\r
+               return 0;\r
+       \r
        nbuckets = pBuckets[0];\r
        iSymCount = pBuckets[1];\r
        pBuckets = &pBuckets[2];\r
index 75565f1..8655377 100644 (file)
@@ -1,5 +1,15 @@
 /*
  */
+#define DEBUG  0
+
+
+#if DEBUG
+# define DEBUG_S       printf
+#else
+# define DEBUG_S(...)
+# define DONT_INCLUDE_SYSCALL_NAMES
+#endif
+
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
@@ -154,31 +164,31 @@ int SendRequest(tRequestHeader *Request, int RequestSize, int ResponseSize)
        {
                 int    i;
                char    *data = (char*)&Request->Params[Request->NParams];
-               printf("Request #%i (%s) -", Request->CallID, casSYSCALL_NAMES[Request->CallID]);
+               DEBUG_S("Request #%i (%s) -", Request->CallID, casSYSCALL_NAMES[Request->CallID]);
                for( i = 0; i < Request->NParams; i ++ )
                {
                        switch(Request->Params[i].Type)
                        {
                        case ARG_TYPE_INT32:
-                               printf(" 0x%08x", *(uint32_t*)data);
+                               DEBUG_S(" 0x%08x", *(uint32_t*)data);
                                data += sizeof(uint32_t);
                                break;
                        case ARG_TYPE_INT64:
-                               printf(" 0x%016llx", *(uint64_t*)data);
+                               DEBUG_S(" 0x%016llx", *(uint64_t*)data);
                                data += sizeof(uint64_t);
                                break;
                        case ARG_TYPE_STRING:
-                               printf(" '%s'", (char*)data);
+                               DEBUG_S(" '%s'", (char*)data);
                                data += Request->Params[i].Length;
                                break;
                        case ARG_TYPE_DATA:
-                               printf(" %p:0x%x", (char*)data, Request->Params[i].Length);
+                               DEBUG_S(" %p:0x%x", (char*)data, Request->Params[i].Length);
                                if( !(Request->Params[i].Flags & ARG_FLAG_ZEROED) )
                                        data += Request->Params[i].Length;
                                break;
                        }
                }
-               printf("\n");
+               DEBUG_S("\n");
        }
        
        // Send it off
@@ -246,7 +256,7 @@ int ReadData(void *Dest, int MaxLength, int Timeout)
                exit(-1);
        }
        
-       printf("%i bytes read from socket\n", ret);
+       DEBUG_S("%i bytes read from socket\n", ret);
        
        return ret;
 }
index 2a63622..5882435 100644 (file)
@@ -271,7 +271,7 @@ uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
        free( req );
        free( retPtrs );
        
-       printf("Return %llx\n", retValue);
+       DEBUG(": %llx\n", retValue);
        
        return retValue;
 }
@@ -289,7 +289,7 @@ int acess_open(const char *Path, int Flags)
                if(!gaSyscall_LocalFPs[ret])    return -1;
                return ret|NATIVE_FILE_MASK;
        }
-       DEBUG("open(\"%s\", 0x%x)\n", Path, Flags);
+       DEBUG("open(\"%s\", 0x%x)", Path, Flags);
        return _Syscall(SYS_OPEN, ">s >i", Path, Flags);
 }
 
@@ -299,26 +299,26 @@ void acess_close(int FD) {
                gaSyscall_LocalFPs[FD & (NATIVE_FILE_MASK-1)] = NULL;
                return ;
        }
-       DEBUG("close(%i)\n", FD);
+       DEBUG("close(%i)", FD);
        _Syscall(SYS_CLOSE, ">i", FD);
 }
 
 int acess_reopen(int FD, const char *Path, int Flags) {
-       DEBUG("reopen(0x%x, \"%s\", 0x%x)\n", FD, Path, Flags);
+       DEBUG("reopen(0x%x, \"%s\", 0x%x)", FD, Path, Flags);
        return _Syscall(SYS_REOPEN, ">i >s >i", FD, Path, Flags);
 }
 
 size_t acess_read(int FD, size_t Bytes, void *Dest) {
        if(FD & NATIVE_FILE_MASK)
                return fread( Dest, Bytes, 1, gaSyscall_LocalFPs[FD & (NATIVE_FILE_MASK-1)] );
-       DEBUG("read(0x%x, 0x%x, *%p)\n", FD, Bytes, Dest);
+       DEBUG("read(0x%x, 0x%x, *%p)", FD, Bytes, Dest);
        return _Syscall(SYS_READ, ">i >i <d", FD, Bytes, Bytes, Dest);
 }
 
 size_t acess_write(int FD, size_t Bytes, void *Src) {
        if(FD & NATIVE_FILE_MASK)
                return fwrite( Src, Bytes, 1, gaSyscall_LocalFPs[FD & (NATIVE_FILE_MASK-1)] );
-       DEBUG("write(0x%x, 0x%x, %p(\"%.*s\"))\n", FD, Bytes, Src, Bytes, (char*)Src);
+       DEBUG("write(0x%x, 0x%x, %p(\"%.*s\"))", FD, Bytes, Src, Bytes, (char*)Src);
        return _Syscall(SYS_WRITE, ">i >i >d", FD, Bytes, Bytes, Src);
 }
 
@@ -332,7 +332,7 @@ int acess_seek(int FD, int64_t Ofs, int Dir) {
                }
                return fseek( gaSyscall_LocalFPs[FD & (NATIVE_FILE_MASK-1)], Ofs, Dir );
        }
-       DEBUG("seek(0x%x, 0x%llx, %i)\n", FD, Ofs, Dir);
+       DEBUG("seek(0x%x, 0x%llx, %i)", FD, Ofs, Dir);
        return _Syscall(SYS_SEEK, ">i >I >i", FD, Ofs, Dir);
 }
 
@@ -390,9 +390,9 @@ int acess_clone(int flags, void *stack)
        extern int fork(void);
        if(flags & CLONE_VM) {
                 int    ret, newID, kernel_tid=0;
-               printf("fork()\n");
+               printf("fork()");
                
-               newID = _Syscall(SYS_FORK, "<i", &kernel_tid);
+               newID = _Syscall(SYS_FORK, "<d", sizeof(int), &kernel_tid);
                ret = fork();
                if(ret < 0)     return ret;
                
@@ -451,7 +451,7 @@ void acess_sleep(void)
 
 int acess_waittid(int TID, int *ExitStatus)
 {
-       return _Syscall(SYS_WAITTID, ">i <i", TID, ExitStatus);
+       return _Syscall(SYS_WAITTID, ">i <d", TID, sizeof(int), &ExitStatus);
 }
 
 int acess_setuid(int ID)

UCC git Repository :: git.ucc.asn.au