Subversion Repositories HelenOS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2008 Jiri Svoboda
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup rtld rtld
  30.  * @brief
  31.  * @{
  32.  */
  33. /**
  34.  * @file
  35.  */
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. #include <fcntl.h>
  41. #include <libadt/list.h>
  42.  
  43. #include <rtld.h>
  44. #include <dynamic.h>
  45. #include <pcb.h>
  46. #include <elf_load.h>
  47. #include <arch.h>
  48. #include <module.h>
  49.  
  50. /** (Eagerly) process all relocation tables in a module.
  51.  *
  52.  * Currently works as if LD_BIND_NOW was specified.
  53.  */
  54. void module_process_relocs(module_t *m)
  55. {
  56.     printf("module_process_relocs('%s')\n", m->dyn.soname);
  57.     if (m->dyn.plt_rel == DT_REL) {
  58.         if (m->dyn.rel != NULL)
  59.             rel_table_process(m, m->dyn.rel, m->dyn.rel_sz);
  60.         /* FIXME: this seems wrong */
  61.         if (m->dyn.jmp_rel != NULL)
  62.             rel_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
  63.     } else { /* (m->dyn.plt_rel == DT_RELA) */
  64.         printf("table type DT_RELA\n");
  65.         if (m->dyn.rela != NULL) {
  66.             printf("non-empty\n");
  67.             rela_table_process(m, m->dyn.rela, m->dyn.rela_sz);
  68.         }
  69.     }
  70. }
  71.  
  72. /** Find module structure by soname.
  73.  *
  74.  * Used primarily to see if a module has already been loaded.
  75.  */
  76. module_t *module_find(char *soname)
  77. {
  78.     link_t *head = &runtime_env.modules_head;
  79.  
  80.     link_t *cur;
  81.     module_t *m;
  82.  
  83.     /* FIXME!! */
  84.     if (soname[0] == '.') return &runtime_env.rtld;
  85.  
  86.     for (cur = head->next; cur != head; cur = cur->next) {
  87.         m = list_get_instance(cur, module_t, modules_link);
  88.         if (strcmp(m->dyn.soname, soname) == 0) {
  89.             return m; /* Found */
  90.         }
  91.     }
  92.    
  93.     return NULL; /* Not found */
  94. }
  95.  
  96. #define NAME_BUF_SIZE 64
  97.  
  98. /** Load a module.
  99.  *
  100.  * Currently this trivially tries to load '/<soname>'.
  101.  */
  102. module_t *module_load(char *soname)
  103. {
  104.     elf_info_t info;
  105.     char name_buf[NAME_BUF_SIZE];
  106.     module_t *m;
  107.     int rc;
  108.    
  109.     m = malloc(sizeof(module_t));
  110.     if (!m) {
  111.         printf("malloc failed\n");
  112.         exit(1);
  113.     }
  114.  
  115.     printf("Requested load of module '%s'\n", soname);
  116.  
  117.     if (strlen(soname) > NAME_BUF_SIZE - 2) {
  118.         printf("soname too long. increase NAME_BUF_SIZE\n");
  119.         exit(1);
  120.     }
  121.  
  122.     /* Prepend soname with slash */
  123.     name_buf[0] = '/';
  124.     strcpy(name_buf + 1, soname);
  125.  
  126.     /* FIXME: need to vary bias / allocate address space */
  127.     m->bias = 0x20000;
  128.     printf("filename:'%s'\n", name_buf);
  129.  
  130.     rc = elf_load_file(name_buf, m->bias, &info);
  131.     if (rc < 0) {
  132.         printf("Failed to load '%s'\n");
  133.         exit(1);
  134.     }
  135.  
  136.     printf("parse dynamic section\n");
  137.     /* Parse ELF .dynamic section. Store info to m->dyn. */
  138.     dynamic_parse(info.dynamic, m->bias, &m->dyn);
  139.     printf("...\n");
  140.  
  141.     /* Insert into the list of loaded modules */
  142.     list_append(&m->modules_link, &runtime_env.modules_head);
  143.  
  144.     return m;
  145. }
  146.  
  147. /** Load all modules on which m (transitively) depends.
  148.  */
  149. void module_load_deps(module_t *m)
  150. {
  151.     elf_dyn_t *dp;
  152.     char *dep_soname;
  153.     module_t *dm;
  154.     size_t n, i;
  155.  
  156.     /* Count direct dependencies */
  157.    
  158.     dp = m->dyn.dynamic;
  159.     n = 0;
  160.  
  161.     while (dp->d_tag != DT_NULL) {
  162.         if (dp->d_tag == DT_NEEDED) ++n;
  163.         ++dp;
  164.     }
  165.  
  166.     /* Create an array of pointers to direct dependencies */
  167.  
  168.     m->n_deps = n;
  169.     if (n > 0)
  170.         m->deps = malloc(n * sizeof(module_t *));
  171.     else
  172.         m->deps = NULL;
  173.  
  174.     if (!m->deps) {
  175.         printf("malloc failed\n");
  176.         exit(1);
  177.     }
  178.  
  179.     i = 0; /* Current dependency index */
  180.     dp = m->dyn.dynamic;
  181.  
  182.     while (dp->d_tag != DT_NULL) {
  183.         if (dp->d_tag == DT_NEEDED) {
  184.             dep_soname = m->dyn.str_tab + dp->d_un.d_val;
  185.             printf("needed:'%s'\n", dep_soname);
  186.  
  187.             dm = module_find(dep_soname);
  188.             if (!dm) {
  189.                 dm = module_load(dep_soname);
  190.                 module_load_deps(dm);
  191.             }
  192.  
  193.             /* Save into deps table */
  194.             m->deps[i++] = dm;
  195.         }
  196.         ++dp;
  197.     }
  198. }
  199.  
  200. void modules_process_relocs(void)
  201. {
  202.     link_t *head = &runtime_env.modules_head;
  203.  
  204.     link_t *cur;
  205.     module_t *m;
  206.  
  207.     for (cur = head->next; cur != head; cur = cur->next) {
  208.         m = list_get_instance(cur, module_t, modules_link);
  209.  
  210.         /* Skip rtld, since it has already been processed */
  211.         if (m != &runtime_env.rtld) {
  212.             module_process_relocs(m);
  213.         }
  214.     }
  215. }
  216.  
  217. /** @}
  218.  */
  219.