Usermode/ld-acess - Fixing elf64 support (and x86-64)
authorJohn Hodge <[email protected]>
Tue, 4 Oct 2011 03:52:16 +0000 (11:52 +0800)
committerJohn Hodge <[email protected]>
Tue, 4 Oct 2011 03:52:16 +0000 (11:52 +0800)
Usermode/Libraries/ld-acess.so_src/arch/x86_64.asm.h
Usermode/Libraries/ld-acess.so_src/elf.c
Usermode/Libraries/ld-acess.so_src/elf64.h
Usermode/Libraries/ld-acess.so_src/lib.c
Usermode/Libraries/ld-acess.so_src/loadlib.c
Usermode/include/stdint.h

index fdfe5e9..2c9d87f 100644 (file)
@@ -80,7 +80,7 @@ _errno:       dw      0       ; Placed in .text, to allow use of relative addressing
 ; System Call - 1 Argument
 %macro SYSCALL1        2
 _SYSCALL_HEAD %1, %2
-       mov rdi, [rbp+2*8]
+;      mov rdi, [rbp+3*8]
        SYSCALL_OP
 _SYSCALL_TAIL
 %endmacro
@@ -88,8 +88,8 @@ _SYSCALL_TAIL
 ; System Call - 2 Arguments
 %macro SYSCALL2        2
 _SYSCALL_HEAD %1, %2
-       mov rdi, [rbp+2*8]
-       mov rsi, [rbp+3*8]
+;      mov rdi, [rbp+3*8]
+;      mov rsi, [rbp+4*8]
        SYSCALL_OP
 _SYSCALL_TAIL
 %endmacro
@@ -97,9 +97,9 @@ _SYSCALL_TAIL
 ; System Call - 3 Arguments
 %macro SYSCALL3        2
 _SYSCALL_HEAD %1, %2
-;      mov rdi, [rbp+2*8]
-;      mov rsi, [rbp+3*8]
-;      mov rdx, [rbp+4*8]
+;      mov rdi, [rbp+3*8]
+;      mov rsi, [rbp+4*8]
+;      mov rdx, [rbp+5*8]
        SYSCALL_OP
 _SYSCALL_TAIL
 %endmacro
index c45c481..75791d6 100644 (file)
@@ -65,9 +65,18 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename)
        void    *pltrel = NULL;
         int    plt_size = 0, plt_type = 0;
 
-       DEBUGS("Elf64Relocate: e_ident = '%.16s'", hdr->e_ident);
-       DEBUGS("Elf64Relocate: e_phoff = %i, e_phnum = %i",
-               hdr->e_phoff, hdr->e_phnum);
+       DEBUGS("Elf64Relocate: hdr = {");
+       DEBUGS("Elf64Relocate:  e_ident = '%.16s'", hdr->e_ident);
+       DEBUGS("Elf64Relocate:  e_type = 0x%x", hdr->e_type);
+       DEBUGS("Elf64Relocate:  e_machine = 0x%x", hdr->e_machine);
+       DEBUGS("Elf64Relocate:  e_version = 0x%x", hdr->e_version);
+       DEBUGS("Elf64Relocate:  e_entry = %p", hdr->e_entry);
+       DEBUGS("Elf64Relocate:  e_phoff = 0x%llx", hdr->e_phoff);
+       DEBUGS("Elf64Relocate:  e_shoff = 0x%llx", hdr->e_shoff);
+       DEBUGS("Elf64Relocate:  e_flags = 0x%x", hdr->e_flags);
+       DEBUGS("Elf64Relocate:  e_ehsize = 0x%x", hdr->e_ehsize);
+       DEBUGS("Elf64Relocate:  e_phentsize = 0x%x", hdr->e_phentsize);
+       DEBUGS("Elf64Relocate:  e_phnum = %i", hdr->e_phnum);
 
        // Scan for the dynamic table (and find the compiled base)
        phtab = Base + hdr->e_phoff;
@@ -122,12 +131,14 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename)
        // Second pass on dynamic table
        for(i = 0; dyntab[i].d_tag != DT_NULL; i ++)
        {
+               DEBUGS("dyntab[%i].d_tag = %i", i, dyntab[i].d_tag);
                switch(dyntab[i].d_tag)
                {
                case DT_SONAME: break;
 
                case DT_NEEDED: {
                        char *libPath = strtab + dyntab[i].d_un.d_val;
+                       DEBUGS("Elf64Relocate: libPath = '%s'", libPath);
                        if(LoadLibrary(libPath, NULL, envp) == 0) {
                                SysDebug("ld-acess - Elf64Relocate: Unable to load '%s'", libPath);
                                return NULL;
@@ -191,11 +202,13 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename)
                        break;
                default:
                        SysDebug("ld-acess - _Elf64DoReloc: Unknown relocation type %i", type);
+                       break;
                }
        }
 
        if( rel )
        {
+               DEBUGS("rel_count = %i", rel_count);
                for( i = 0; i < rel_count; i ++ )
                {
                        uint64_t *ptr = (void *)( rel[i].r_offset + baseDiff );
@@ -205,6 +218,7 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename)
 
        if( rela )
        {
+               DEBUGS("rela_count = %i", rela_count);
                for( i = 0; i < rela_count; i ++ )
                {
                        _Elf64DoReloc( rela[i].r_info, (void *)( rela[i].r_offset + baseDiff ), rela[i].r_addend );
@@ -216,6 +230,7 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename)
                if( plt_type == DT_REL ) {
                        Elf64_Rel       *plt = pltrel;
                         int    count = plt_size / sizeof(Elf64_Rel);
+                       DEBUGS("plt rel count = %i", count);
                        for( i = 0; i < count; i ++ )
                        {
                                uint64_t *ptr = (void *)( plt[i].r_offset + baseDiff );
@@ -225,6 +240,7 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename)
                else {
                        Elf64_Rela      *plt = pltrel;
                         int    count = plt_size / sizeof(Elf64_Rela);
+                       DEBUGS("plt rela count = %i", count);
                        for( i = 0; i < count; i ++ )
                        {
                                _Elf64DoReloc( plt[i].r_info, (void *)(plt[i].r_offset + baseDiff), plt[i].r_addend);
@@ -232,6 +248,7 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename)
                }
        }
 
+       DEBUGS("Elf64Relocate: Relocations done, return %p", (void *)(hdr->e_entry + baseDiff));
        return (void *)(hdr->e_entry + baseDiff);
 }
 
index 237b75c..4fe99ae 100644 (file)
@@ -11,6 +11,7 @@
 
 typedef uint16_t       Elf64_Half;
 typedef uint32_t       Elf64_Word;
+
 typedef uint64_t       Elf64_Addr;
 typedef uint64_t       Elf64_Off;
 typedef uint64_t       Elf64_Xword;
@@ -32,7 +33,7 @@ typedef struct
        Elf64_Half      e_shentsize;
        Elf64_Half      e_shnum;
        Elf64_Half      e_shstrndx;
-} Elf64_Ehdr;
+} __attribute__((packed)) Elf64_Ehdr;
 
 typedef struct
 {
index 6d66da0..27d6ff2 100644 (file)
@@ -64,9 +64,9 @@ int memcmp(const void *p1, const void *p2, int len)
 int file_exists(const char *filename)
 {
         int    fd;
-        //fd = open(filename, OPENFLAG_READ);
-        fd = open(filename, 0);
-        if(fd == -1)   return 0;
-        close(fd);
-        return 1;
+       //fd = open(filename, OPENFLAG_READ);
+       fd = open(filename, 0);
+       if(fd == -1)    return 0;
+       close(fd);
+       return 1;
 }
index 529e0a0..9a1bc10 100644 (file)
@@ -4,7 +4,7 @@
 */
 #include "common.h"
 
-#define DEBUG  0
+#define DEBUG  1
 
 #if DEBUG
 # define DEBUGS(v...)  SysDebug(v)
@@ -61,36 +61,37 @@ void *LoadLibrary(const char *SoName, const char *SearchDir, char **envp)
        void    *base;
        void    (*fEntry)(void *, int, char *[], char**);
        
-       DEBUGS("LoadLibrary: (filename='%s', envp=0x%x)\n", filename, envp);
+       DEBUGS("LoadLibrary: (SoName='%s', SearchDir='%s', envp=0x%x)", SoName, SearchDir, envp);
        
        // Create Temp Name
        filename = FindLibrary(sTmpName, SoName, SearchDir);
        if(filename == NULL) {
-               DEBUGS("LoadLibrary: RETURN 0\n");
+               DEBUGS("LoadLibrary: RETURN 0");
                return 0;
        }
-       DEBUGS(" LoadLibrary: filename='%s'\n", filename);
+       DEBUGS(" LoadLibrary: filename='%s'", filename);
        
        if( (base = IsFileLoaded(filename)) )
                return base;
-       
+
+       DEBUGS(" LoadLibrary: SysLoadBin()");   
        // Load Library
        base = SysLoadBin(filename, (void**)&fEntry);
        if(!base) {
-               DEBUGS("LoadLibrary: RETURN 0\n");
+               DEBUGS("LoadLibrary: RETURN 0");
                return 0;
        }
        
-       DEBUGS(" LoadLibrary: iArg=%p, iEntry=0x%x\n", base, fEntry);
+       DEBUGS(" LoadLibrary: iArg=%p, iEntry=0x%x", base, fEntry);
        
        // Load Symbols
        fEntry = DoRelocate( base, envp, filename );
        
        // Call Entrypoint
-       DEBUGS(" LoadLibrary: '%s' Entry 0x%x\n", SoName, fEntry);
+       DEBUGS(" LoadLibrary: '%s' Entry 0x%x", SoName, fEntry);
        fEntry(base, 0, NULL, envp);
        
-       DEBUGS("LoadLibrary: RETURN 1\n");
+       DEBUGS("LoadLibrary: RETURN 1");
        return base;
 }
 
@@ -148,7 +149,7 @@ void AddLoaded(const char *File, void *base)
        strcpy(name, File);
        gLoadedLibraries[i].Name = name;
        gsNextAvailString = &name[length+1];
-       DEBUGS("'%s' (0x%x) loaded as %i\n", name, base, i);
+       DEBUGS("'%s' (0x%x) loaded as %i", name, base, i);
        return;
 }
 
index 37b0c84..6238a63 100644 (file)
@@ -6,12 +6,12 @@
 //typedef unsigned int uint;
 typedef unsigned char  uint8_t;
 typedef unsigned short uint16_t;
-typedef unsigned long  uint32_t;
+typedef unsigned int   uint32_t;
 typedef unsigned long long     uint64_t;
 
 typedef signed char            int8_t;
 typedef signed short   int16_t;
-typedef signed long            int32_t;
+typedef signed int             int32_t;
 typedef signed long long       int64_t;
 
 #ifdef __LP64__

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