Subversion Repositories Kolibri OS

Rev

Rev 3809 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3809 Rev 3900
Line 68... Line 68...
68
    char *path;
68
    char *path;
69
    int   path_len;
69
    int   path_len;
70
}dll_path_t;
70
}dll_path_t;
71
 
71
 
Line 72... Line 72...
72
module_t* load_module(const char *name);
72
module_t* load_library(const char *name);
Line 73... Line 73...
73
 
73
 
Line 74... Line 74...
74
LIST_HEAD(path_list);
74
LIST_HEAD(path_list);
75
 
75
 
Line 143... Line 143...
143
    PIMAGE_DOS_HEADER        dos;
143
    PIMAGE_DOS_HEADER        dos;
144
    PIMAGE_NT_HEADERS32      nt;
144
    PIMAGE_NT_HEADERS32      nt;
145
    PIMAGE_EXPORT_DIRECTORY  exp;
145
    PIMAGE_EXPORT_DIRECTORY  exp;
Line 146... Line 146...
146
 
146
 
147
    struct app_hdr          *header;
-
 
148
    dll_path_t              *path;
-
 
149
 
-
 
150
#if 0
147
    struct app_hdr  *header = NULL;
151
    dll_path_t *path;
148
    dll_path_t      *path;
152
    int len;
149
    int             len;
Line -... Line 150...
-
 
150
    char            *p;
-
 
151
 
153
    char *p;
152
#if 0
154
 
153
 
155
    if(__appenv_size)
154
    if(__appenv_size)
156
    {
155
    {
157
        char *env;
156
        char *env;
Line 195... Line 194...
195
                else break;
194
                else break;
196
            };
195
            };
197
        };
196
        };
198
    };
197
    };
199
 
198
#endif
200
    header = (struct app_hdr*)NULL;
199
 
201
 
-
 
Line 202... Line 200...
202
    len = strrchr(header->path, '/') - header->path+1;
200
    len = strrchr(header->path, '/') - header->path+1;
203
    p = (char*)malloc(len+1);
201
    p = (char*)malloc(len+1);
204
    memcpy(p, header->path, len);
202
    memcpy(p, header->path, len);
205
    p[len]=0;
203
    p[len]=0;
Line 210... Line 208...
210
    path->path_len = len;
208
    path->path_len = len;
211
    DBG("add libraries path %s\n", path->path);
209
    DBG("add libraries path %s\n", path->path);
212
    list_add_tail(&path->list, &path_list);
210
    list_add_tail(&path->list, &path_list);
213
 
211
 
Line 214... Line -...
214
#endif
-
 
Line 215... Line 212...
215
 
212
 
216
    path = (dll_path_t*)malloc(sizeof(dll_path_t));
213
    path = (dll_path_t*)malloc(sizeof(dll_path_t));
217
    INIT_LIST_HEAD(&path->list);
214
    INIT_LIST_HEAD(&path->list);
218
    path->path = "/kolibrios/lib/";
215
    path->path = "/kolibrios/lib/";
Line 241... Line 238...
241
                        nt->OptionalHeader.DataDirectory[0].VirtualAddress);
238
                        nt->OptionalHeader.DataDirectory[0].VirtualAddress);
242
 
239
 
Line 243... Line 240...
243
};
240
};
Line 244... Line -...
244
 
-
 
245
const module_t* find_module(const char *name)
-
 
246
{
-
 
247
    module_t* mod = &libc_dll;
-
 
248
 
-
 
249
    do
-
 
250
    {
-
 
251
        if( !strncmp(name, mod->img_name, 16))
-
 
252
            return mod;
-
 
253
        mod = (module_t*)mod->list.next;
-
 
254
    }while(mod != &libc_dll);
-
 
255
 
-
 
256
    return load_module(name);
-
 
257
};
-
 
258
 
241
 
259
static inline void sec_copy(void *dst, void *src, size_t len)
242
static inline void sec_copy(void *dst, void *src, size_t len)
260
{
243
{
261
    __asm__ __volatile__ (
244
    __asm__ __volatile__ (
262
    "shrl $2, %%ecx         \n\t"
245
    "shrl $2, %%ecx         \n\t"
Line 395... Line 378...
395
        libname=MakePtr(char*,imp->Name, img_base);
378
        libname=MakePtr(char*,imp->Name, img_base);
Line 396... Line 379...
396
 
379
 
Line 397... Line 380...
397
        DBG("import from %s\n",libname);
380
        DBG("import from %s\n",libname);
398
 
381
 
399
        api = find_module(libname);
382
        api = load_library(libname);
400
        if(unlikely(api == NULL))
383
        if(unlikely(api == NULL))
401
        {
384
        {
402
            printf("library %s not found\n", libname);
385
            printf("library %s not found\n", libname);
Line 623... Line 606...
623
 
606
 
Line 624... Line 607...
624
    return function;
607
    return function;
625
};
608
};
Line 626... Line -...
626
 
-
 
627
 
609
 
628
module_t* load_module(const char *name)
-
 
629
{
-
 
630
    dll_path_t *dllpath;
-
 
631
 
-
 
632
    char        *path;
-
 
633
    int         len;
-
 
634
 
-
 
635
    len = strlen(name);
-
 
636
 
-
 
637
    list_for_each_entry(dllpath, &path_list, list)
610
static void *load_lib_internal(const char *path)
638
    {
611
{
639
        PIMAGE_DOS_HEADER        dos;
612
    PIMAGE_DOS_HEADER        dos;
640
        PIMAGE_NT_HEADERS32      nt;
613
    PIMAGE_NT_HEADERS32      nt;
Line 641... Line -...
641
        PIMAGE_EXPORT_DIRECTORY  exp;
-
 
642
 
614
    PIMAGE_EXPORT_DIRECTORY  exp;
643
        module_t *module;
615
 
644
        ufile_t   uf;
616
    ufile_t   uf;
645
        void     *raw_img;
617
    void     *raw_img;
646
        size_t    raw_size;
-
 
647
        void     *img_base;
-
 
648
 
-
 
649
        path = alloca(len+dllpath->path_len+1);
-
 
650
        memcpy(path, dllpath->path, dllpath->path_len);
-
 
651
 
-
 
652
        memcpy(path+dllpath->path_len, name, len);
-
 
653
        path[len+dllpath->path_len]=0;
-
 
Line 654... Line 618...
654
 
618
    size_t    raw_size;
655
//        printf("%s %s\n", path);
619
    void     *img_base = NULL;
656
 
620
 
Line 657... Line 621...
657
        uf = load_file(path);
621
    uf = load_file(path);
658
        raw_img  = uf.data;
622
    raw_img  = uf.data;
659
        raw_size = uf.size;
-
 
Line 660... Line 623...
660
 
623
    raw_size = uf.size;
661
        if(raw_img == NULL)
624
 
662
            continue;
625
    if(raw_img == NULL)
663
 
626
        return NULL;
664
 
627
 
665
        if( validate_pe(raw_img, raw_size, 0) == 0)
628
    if( validate_pe(raw_img, raw_size, 0) == 0)
Line 666... Line 629...
666
        {
629
    {
667
            printf("invalide module %s\n", path);
630
        printf("invalide module %s\n", path);
Line 668... Line 631...
668
            user_free(raw_img);
631
        user_free(raw_img);
669
            continue;
-
 
670
        };
632
        return NULL;
-
 
633
    };
-
 
634
 
-
 
635
    img_base = create_image(raw_img);
-
 
636
    user_free(raw_img);
-
 
637
 
-
 
638
    if( unlikely(img_base == NULL) )
-
 
639
        printf("cannot create image %s\n",path);
-
 
640
 
-
 
641
    return img_base;
-
 
642
}
-
 
643
 
-
 
644
module_t* load_library(const char *name)
-
 
645
{
-
 
646
    PIMAGE_DOS_HEADER        dos;
-
 
647
    PIMAGE_NT_HEADERS32      nt;
-
 
648
    PIMAGE_EXPORT_DIRECTORY  exp;
-
 
649
 
-
 
650
    module_t    *module, *mod = &libc_dll;
-
 
651
    dll_path_t  *dllpath;
-
 
652
    char        *path;
-
 
653
    int          len;
-
 
654
    char        *libname, *tmp;
-
 
655
    void        *img_base;
-
 
656
 
-
 
657
 
-
 
658
/*  check for already loaded libraries  */
-
 
659
 
-
 
660
    tmp = strrchr(name, '/');
-
 
661
    libname = path = tmp != NULL ? tmp+1 : (char*)name;
-
 
662
 
-
 
663
//    printf("path %s\n", path);
-
 
664
 
-
 
665
    do
-
 
666
    {
-
 
667
        if( !strncmp(path, mod->img_name, 16))
-
 
668
            return mod;
-
 
669
        mod = (module_t*)mod->list.next;
-
 
670
    }while(mod != &libc_dll);
-
 
671
 
-
 
672
    if(name[0] == '/')
-
 
673
    {
-
 
674
        path = (char*)name;
-
 
675
        img_base = load_lib_internal(path);
-
 
676
    }
-
 
677
    else
-
 
678
    {
-
 
679
        len = strlen(libname);
-
 
680
        list_for_each_entry(dllpath, &path_list, list)
-
 
681
        {
-
 
682
            path = alloca(len+dllpath->path_len+1);
-
 
683
            memcpy(path, dllpath->path, dllpath->path_len);
-
 
684
 
-
 
685
            memcpy(path+dllpath->path_len, libname, len);
671
 
686
            path[len+dllpath->path_len]=0;
672
        img_base = create_image(raw_img);
687
 
-
 
688
            printf("%s\n", path);
-
 
689
 
-
 
690
            img_base = load_lib_internal(path);
-
 
691
 
-
 
692
            if( unlikely(img_base == NULL) )
-
 
693
                continue;
-
 
694
        };
Line 673... Line 695...
673
        user_free(raw_img);
695
    }
Line 674... Line 696...
674
 
696
 
675
        if( unlikely(img_base == NULL) )
697
    if( unlikely(img_base == NULL) )
676
        {
698
    {
677
            printf("cannot create image %s\n",path);
-
 
678
            continue;
699
        printf("unable to load %s\n", name);
679
        };
700
        return NULL;
Line 680... Line 701...
680
 
701
    };
Line 681... Line 702...
681
        module = (module_t*)malloc(sizeof(module_t));
702
 
682
 
703
    module = (module_t*)malloc(sizeof(module_t));
683
        if(unlikely(module == NULL))
704
 
684
        {
705
    if(unlikely(module == NULL))
685
            printf("%s epic fail: no enough memory\n",__FUNCTION__);
706
    {
Line 717... Line 738...
717
            imp = MakePtr(PIMAGE_IMPORT_DESCRIPTOR, img_base,
738
        imp = MakePtr(PIMAGE_IMPORT_DESCRIPTOR, img_base,
718
                        nt->OptionalHeader.DataDirectory[1].VirtualAddress);
739
                    nt->OptionalHeader.DataDirectory[1].VirtualAddress);
Line 719... Line 740...
719
 
740
 
720
            if(link_image(img_base, imp) == 0)
741
        if(link_image(img_base, imp) == 0)
Line 721... Line 742...
721
                return NULL;
742
            goto err2;
722
 
743
 
-
 
744
        dll_startup = get_proc_address(module, "DllStartup");
723
            dll_startup = get_proc_address(module, "DllStartup");
745
        if( dll_startup )
724
            if( dll_startup )
746
        {
-
 
747
            if( 0 == dll_startup(module, 1))
725
                if( 0 == dll_startup(module, 1))
748
                goto err2;
-
 
749
        }
726
                    return NULL;
750
    };
-
 
751
 
-
 
752
    return module;
-
 
753
 
-
 
754
err2:
-
 
755
    list_del(&module->list);
-
 
756
    free(module->img_name);
727
        };
757
    free(module->img_path);
-
 
758
    free(module);
728
        return module;
759
err1:
-
 
760
    user_free(img_base);
729
    };
761
    return NULL;