home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / elf / d-link / libdl / dlib.c next >
Encoding:
C/C++ Source or Header  |  1994-12-05  |  10.6 KB  |  403 lines

  1. /*
  2.  * libdl.c
  3.  * 
  4.  * Functions required for dlopen et. al.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <dlfcn.h>
  9. #include <stdlib.h>
  10. #include <sys/mman.h>
  11. #include "hash.h"
  12. #include "string.h"
  13. #include "linuxelf.h"
  14.  
  15. extern int _dl_error_number;
  16. extern void * (*_dl_malloc_function)(size_t size);
  17.  
  18. static struct dyn_elf * handles;
  19. static int do_fixup(struct elf_resolve * tpnt, int flag);
  20.  
  21. void * _dlopen(char * filename, int flag);
  22. const char * _dlerror(void);
  23. void * _dlsym(void *, char *);
  24. int _dlclose(void *);
  25.  
  26. static const char * dl_error_names[] = {
  27.     "",
  28.     "File not found",
  29.     "Unable to open /dev/null",
  30.     "Not an ELF file",
  31.     "Not i386 binary",
  32.     "Not an ELF shared library",
  33.     "Unable to mmap file",
  34.     "No dynamic section",
  35.     "Unable to process RELA relocs",
  36.     "Bad handle",
  37.     "Unable to resolve symbol"
  38. };
  39.     
  40. static void dl_cleanup()
  41. {
  42.     struct dyn_elf * rpnt, *rpnt1;
  43.     
  44.     for(rpnt = handles; rpnt; rpnt = rpnt1)
  45.     {
  46.         rpnt1 = rpnt->next_handle;
  47.         dlclose(rpnt);
  48.     }
  49. }
  50.  
  51. void * _dlopen(char * libname, int flag)
  52. {
  53.     struct elf_resolve * tpnt;
  54.     struct dyn_elf * rpnt;
  55.     struct dyn_elf * dyn_chain;
  56.     static int dl_init = 0;
  57.     int (*dl_elf_init)(void);
  58.  
  59.     /* Have the dynamic linker use the regular malloc function now */
  60.     if (!dl_init) {
  61.         dl_init++;
  62.         _dl_malloc_function = malloc;
  63.         atexit(dl_cleanup);
  64.     }
  65.  
  66.     /* Cover the trivial case first */
  67.     if (!libname) return _dl_symbol_tables;
  68.  
  69.     if(!(tpnt = _dl_load_shared_library(NULL, libname)))
  70.         return NULL;
  71.  
  72.     tpnt->usage_count++;
  73.     dyn_chain = rpnt = 
  74.        (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  75.     _dl_memset (rpnt, 0, sizeof(*rpnt));
  76.     rpnt->dyn = tpnt;
  77.     tpnt->symbol_scope = dyn_chain;
  78.     
  79.     if (handles)
  80.         handles->next_handle = rpnt;
  81.     handles = rpnt;
  82.  
  83.     /*
  84.      * OK, we have the requested file in memory.  Now check for
  85.      * any other requested files that may also be required.
  86.      */
  87.       {
  88.         struct elf_resolve *tcurr;
  89.         struct elf_resolve * tpnt1;
  90.         struct dynamic * dpnt;
  91.         char * lpnt;
  92.  
  93.         tcurr = tpnt;
  94.         do{
  95.           for(dpnt = (struct dynamic *) tcurr->dynamic_addr; dpnt->d_tag; dpnt++)
  96.         {
  97.       
  98.           if(dpnt->d_tag == DT_NEEDED)
  99.             {
  100.               lpnt = tcurr->loadaddr + tcurr->dynamic_info[DT_STRTAB] + 
  101.             dpnt->d_un.d_val;
  102.               if(!(tpnt1 = _dl_load_shared_library(_dl_symbol_tables, lpnt)))
  103.             goto oops;
  104.  
  105.               rpnt->next = 
  106.                  (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  107.               _dl_memset (rpnt->next, 0, sizeof (*(rpnt->next)));
  108.               rpnt = rpnt->next;
  109.               tpnt1->usage_count++;
  110.               if (!tpnt1->symbol_scope) tpnt1->symbol_scope = dyn_chain;
  111.               rpnt->dyn = tpnt1;
  112.             };
  113.         }
  114.           
  115.           tcurr = tcurr->next;
  116.         } while(tcurr);
  117.       }
  118.      
  119.     /*
  120.      * OK, now attach the entire chain at the end
  121.      */
  122.  
  123.     rpnt->next = _dl_symbol_tables;
  124.  
  125.     if (do_fixup(tpnt, flag)) {
  126.       _dl_error_number = DL_NO_SYMBOL;
  127.       goto oops;
  128.     }
  129.  
  130.     for(rpnt = dyn_chain; rpnt; rpnt = rpnt->next)
  131.         {
  132.         tpnt = rpnt->dyn;
  133.           /* Apparently crt1 for the application is responsible for handling this.
  134.            * We only need to run the init/fini for shared libraries
  135.            */
  136.            if (tpnt->libtype == elf_executable) continue;
  137.           if (tpnt->init_flag & INIT_FUNCS_CALLED) continue;
  138.           tpnt->init_flag |= INIT_FUNCS_CALLED;
  139.       
  140.           if(tpnt->dynamic_info[DT_INIT]) {
  141.         dl_elf_init = (int (*)(void)) (tpnt->loadaddr + 
  142.                         tpnt->dynamic_info[DT_INIT]);
  143.         (*dl_elf_init)();
  144.           }
  145.        }
  146.     return (void *) dyn_chain;
  147. oops:
  148.     /* Something went wrong.  Clean up and return NULL. */
  149.     dlclose (dyn_chain);
  150.     return NULL;
  151. }
  152.  
  153. static int do_fixup(struct elf_resolve * tpnt, int flag)
  154. {
  155.   int goof = 0;
  156.   if(tpnt->next) goof += do_fixup(tpnt->next, flag);
  157.  
  158.   if(tpnt->dynamic_info[DT_REL]) {
  159.     if (tpnt->init_flag & RELOCS_DONE) return goof;
  160.     tpnt->init_flag |= RELOCS_DONE;
  161.    
  162.     goof += _dl_parse_relocation_information(tpnt, tpnt->dynamic_info[DT_REL],
  163.                          tpnt->dynamic_info[DT_RELSZ], 0);
  164.   }
  165.   if(tpnt->dynamic_info[DT_JMPREL])
  166.     {
  167.       if (tpnt->init_flag & JMP_RELOCS_DONE) return goof;
  168.       tpnt->init_flag |= JMP_RELOCS_DONE;
  169.       
  170.       if(flag == RTLD_LAZY)
  171.     _dl_parse_lazy_relocation_information(tpnt, tpnt->dynamic_info[DT_JMPREL],
  172.                           tpnt->dynamic_info[DT_PLTRELSZ], 0);
  173.       else
  174.     goof +=  _dl_parse_relocation_information(tpnt,
  175.                           tpnt->dynamic_info[DT_JMPREL],
  176.                           tpnt->dynamic_info[DT_PLTRELSZ], 0);
  177.     };
  178.   if(tpnt->dynamic_info[DT_RELA]) {
  179.     /* FIXME - return some error code here */
  180.     goof++;
  181.   };
  182.   return goof;
  183. }
  184.  
  185. void * _dlsym(void * vhandle, char * name)
  186. {    
  187.     struct dyn_elf * handle;
  188.     struct dyn_elf * rpnt;
  189.     /* First of all verify that we have a real handle
  190.     of some kind.  Return NULL if not a valid handle. */
  191.  
  192.     handle = (struct dyn_elf *) vhandle;
  193.  
  194.     for(rpnt = handle; rpnt; rpnt = rpnt->next_handle)
  195.         if(rpnt == handle) break;
  196.     
  197.     if (!rpnt) {
  198.         _dl_error_number = DL_BAD_HANDLE;
  199.         return NULL;
  200.     }
  201.         
  202.     return _dl_find_hash(name, handle, 1, NULL, 0);
  203. }
  204.  
  205. int _dlclose(void * vhandle)
  206. {
  207.     struct dyn_elf * rpnt, *rpnt1;
  208.     struct dyn_elf *spnt, *spnt1;
  209.     struct elf_phdr * ppnt;
  210.     struct elf_resolve * tpnt;
  211.     int (*dl_elf_fini)(void);
  212.     struct dyn_elf * handle;
  213.     unsigned int end;
  214.     int i = 0;
  215.  
  216.     handle = (struct dyn_elf *) vhandle;
  217.     rpnt1 = NULL;
  218.     for(rpnt = handles; rpnt; rpnt = rpnt->next_handle)
  219.     {
  220.         if(rpnt == handle) {
  221.             break;
  222.         }
  223.         rpnt1 = rpnt;
  224.     }
  225.     
  226.     if (!rpnt) {
  227.         _dl_error_number = DL_BAD_HANDLE;
  228.         return 1;
  229.     }
  230.  
  231.     /* OK, this is a valid handle - now close out the file */
  232.     for(spnt = handle; spnt; spnt = spnt1)
  233.     {
  234.         spnt1 = spnt->next;
  235.  
  236.         /* We appended the module list to the end - when we get back here, 
  237.          quit. The access counts were not adjusted to account for being here. */
  238.         if (spnt == _dl_symbol_tables) break;
  239.         if(spnt->dyn->usage_count==1 && spnt->dyn->libtype == loaded_file) {
  240.         tpnt = spnt->dyn;
  241.         /* Apparently crt1 for the application is responsible for handling this.
  242.          * We only need to run the init/fini for shared libraries
  243.          */
  244.  
  245.         if(tpnt->dynamic_info[DT_FINI]) {
  246.             dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + 
  247.                            tpnt->dynamic_info[DT_FINI]);
  248.             (*dl_elf_fini)();
  249.         }    
  250.         }
  251.     }
  252.     if(rpnt1)
  253.         rpnt1->next_handle = rpnt->next_handle;
  254.     else
  255.         handles = rpnt->next_handle;
  256.     
  257.     /* OK, this is a valid handle - now close out the file */
  258.     for(rpnt = handle; rpnt; rpnt = rpnt1)
  259.       {
  260.         rpnt1 = rpnt->next;
  261.  
  262.         /* We appended the module list to the end - when we get back here, 
  263.            quit. The access counts were not adjusted to account for being here. */
  264.         if (rpnt == _dl_symbol_tables) break;
  265.  
  266.         rpnt->dyn->usage_count--;
  267.         if(rpnt->dyn->usage_count == 0 && rpnt->dyn->libtype == loaded_file)
  268.         {
  269.             tpnt = rpnt->dyn;
  270.               /* Apparently crt1 for the application is responsible for handling this.
  271.                * We only need to run the init/fini for shared libraries
  272.                */
  273. #if 0
  274. /* We have to do this above, before we start closing objects.
  275. Otherwise when the needed symbols for _fini handling are
  276. resolved a coredump would occur. Rob Ryan (robr@cmu.edu)*/
  277.               if(tpnt->dynamic_info[DT_FINI]) {
  278.             dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + 
  279.                             tpnt->dynamic_info[DT_FINI]);
  280.             (*dl_elf_fini)();
  281.               }    
  282. #endif
  283.               end = 0;
  284.             for(i = 0, ppnt = rpnt->dyn->ppnt; 
  285.                 i < rpnt->dyn->n_phent; ppnt++, i++) {
  286.                 if (ppnt->p_type != PT_LOAD) continue;
  287.                 if (end < ppnt->p_vaddr + ppnt->p_memsz)
  288.                     end = ppnt->p_vaddr + ppnt->p_memsz;
  289.             }
  290.             munmap (rpnt->dyn->loadaddr, end);
  291.             /* Next, remove rpnt->dyn from the loaded_module list */
  292.             if (_dl_loaded_modules == rpnt->dyn)
  293.             {
  294.               _dl_loaded_modules = rpnt->dyn->next;
  295.               if (_dl_loaded_modules)
  296.                 _dl_loaded_modules->prev = 0;
  297.             }
  298.             else
  299.               for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
  300.                 if (tpnt->next == rpnt->dyn) {
  301.                   tpnt->next = tpnt->next->next;
  302.                   if (tpnt->next) 
  303.                     tpnt->next->prev = tpnt;
  304.                   break;
  305.                 }
  306.             free(rpnt->dyn);
  307.               }
  308.         free(rpnt);
  309.       }
  310.     return 0;
  311. }
  312.  
  313. const char * _dlerror()
  314. {
  315.     const char * retval;
  316.     if(!_dl_error_number) return NULL;
  317.     retval = dl_error_names[_dl_error_number];
  318.     _dl_error_number = 0;
  319.     return retval;
  320. }
  321.  
  322. /* Generate the correct symbols that we need. */
  323. #ifndef IBCS_COMPATIBLE
  324. #if 1
  325. #pragma weak dlopen = _dlopen
  326. #pragma weak dlerror = _dlerror
  327. #pragma weak dlclose = _dlclose
  328. #pragma weak dlsym = _dlsym
  329. #else
  330. __asm__(".weak dlopen;dlopen=_dlopen");
  331. __asm__(".weak dlerror;dlerror=_dlerror");
  332. __asm__(".weak dlclose;dlclose=_dlclose");
  333. __asm__(".weak dlsym;dlsym=_dlsym");
  334. #endif
  335. #endif
  336.  
  337. /* This is a real hack.  We need access to the dynamic linker, but we
  338. also need to make it possible to link against this library without any
  339. unresolved externals.  We provide these weak symbols to make the link
  340. possible, but at run time the normal symbols are accessed. */
  341.  
  342. static int foobar()
  343. {
  344.     fprintf(stderr,"libdl library not correctly linked\n");
  345.     exit(1);
  346. }
  347.  
  348. static int foobar1 = 0; /* Use as pointer */
  349.  
  350. #ifndef IBCS_COMPATIBLE
  351. #if 1
  352. #pragma weak _dl_find_hash = foobar
  353. #pragma weak _dl_symbol_tables = foobar1
  354. #pragma weak _dl_loaded_modules = foobar1
  355. #pragma weak _dl_error_number = foobar1
  356. #pragma weak _dl_load_shared_library = foobar
  357. #pragma weak _dl_malloc_function = foobar1
  358. #pragma weak _dl_parse_relocation_information = foobar
  359. #pragma weak _dl_parse_lazy_relocation_information = foobar
  360. #else
  361. __asm__(".weak _dl_find_hash; _dl_find_hash = foobar");
  362. __asm__(".weak _dl_symbol_tables; _dl_symbol_tables = foobar1");
  363. __asm__(".weak _dl_loaded_modules; _dl_loaded_modules = foobar1");
  364. __asm__(".weak _dl_error_number; _dl_error_number = foobar1");
  365. __asm__(".weak _dl_load_shared_library; _dl_load_shared_library = foobar");
  366. __asm__(".weak _dl_malloc_function; _dl_malloc_function = foobar1");
  367. __asm__(".weak _dl_parse_relocation_information; _dl_parse_relocation_information = foobar");
  368. __asm__(".weak _dl_parse_lazy_relocation_information; _dl_parse_lazy_relocation_information = foobar");
  369. #endif
  370. #endif
  371.  
  372. /*
  373.  * Dump information to stderrr about the current loaded modules
  374.  */
  375. static char * type[] = {"Lib","Exe","Int","Mod"};
  376.  
  377. void _dlinfo()
  378. {
  379.     struct elf_resolve * tpnt;
  380.     struct dyn_elf * rpnt, *hpnt;
  381.     fprintf(stderr, "List of loaded modules\n");
  382.     /* First start with a complete list of all of the loaded files. */
  383.     for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
  384.         fprintf(stderr,"\t%8.8x %8.8x %8.8x %s %d %s\n", tpnt->loadaddr, tpnt,
  385.             tpnt->symbol_scope,
  386.             type[tpnt->libtype],
  387.             tpnt->usage_count, 
  388.             tpnt->libname);
  389.  
  390.     /* Next dump the module list for the application itself */
  391.     fprintf(stderr,"\nModules for application (%x):\n", _dl_symbol_tables);
  392.     for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
  393.         fprintf(stderr,"\t%8.8x %s\n", rpnt->dyn, rpnt->dyn->libname);
  394.  
  395.     for (hpnt = handles; hpnt; hpnt = hpnt->next_handle)
  396.     {
  397.         fprintf(stderr,"Modules for handle %x\n", hpnt);
  398.         for(rpnt = hpnt; rpnt; rpnt = rpnt->next)
  399.             fprintf(stderr,"\t%8.8x %s\n", rpnt->dyn, rpnt->dyn->libname);
  400.     }
  401. }
  402.  
  403.