Subversion Repositories HelenOS-historic

Rev

Rev 1248 | Rev 1411 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (C) 2006 Sergey Bondari
  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. /**
  30.  * @file    elf.c
  31.  * @brief   Kernel ELF loader.
  32.  */
  33.  
  34. #include <elf.h>
  35. #include <debug.h>
  36. #include <arch/types.h>
  37. #include <typedefs.h>
  38. #include <mm/as.h>
  39. #include <mm/frame.h>
  40. #include <mm/slab.h>
  41. #include <align.h>
  42. #include <memstr.h>
  43. #include <macros.h>
  44.  
  45. static char *error_codes[] = {
  46.     "no error",
  47.     "invalid image",
  48.     "address space error",
  49.     "incompatible image",
  50.     "unsupported image type",
  51.     "irrecoverable error"
  52. };
  53.  
  54. static int segment_header(elf_segment_header_t *entry, elf_header_t *elf, as_t *as);
  55. static int section_header(elf_section_header_t *entry, elf_header_t *elf, as_t *as);
  56. static int load_segment(elf_segment_header_t *entry, elf_header_t *elf, as_t *as);
  57.  
  58. /** ELF loader
  59.  *
  60.  * @param header Pointer to ELF header in memory
  61.  * @param as Created and properly mapped address space
  62.  * @return EE_OK on success
  63.  */
  64. int elf_load(elf_header_t *header, as_t * as)
  65. {
  66.     int i, rc;
  67.  
  68.     /* Identify ELF */
  69.     if (header->e_ident[EI_MAG0] != ELFMAG0 || header->e_ident[EI_MAG1] != ELFMAG1 ||
  70.         header->e_ident[EI_MAG2] != ELFMAG2 || header->e_ident[EI_MAG3] != ELFMAG3) {
  71.         return EE_INVALID;
  72.     }
  73.    
  74.     /* Identify ELF compatibility */
  75.     if (header->e_ident[EI_DATA] != ELF_DATA_ENCODING || header->e_machine != ELF_MACHINE ||
  76.         header->e_ident[EI_VERSION] != EV_CURRENT || header->e_version != EV_CURRENT ||
  77.         header->e_ident[EI_CLASS] != ELF_CLASS) {
  78.         return EE_INCOMPATIBLE;
  79.     }
  80.  
  81.     if (header->e_phentsize != sizeof(elf_segment_header_t))
  82.         return EE_INCOMPATIBLE;
  83.  
  84.     if (header->e_shentsize != sizeof(elf_section_header_t))
  85.         return EE_INCOMPATIBLE;
  86.  
  87.     /* Check if the object type is supported. */
  88.     if (header->e_type != ET_EXEC)
  89.         return EE_UNSUPPORTED;
  90.  
  91.     /* Walk through all segment headers and process them. */
  92.     for (i = 0; i < header->e_phnum; i++) {
  93.         rc = segment_header(&((elf_segment_header_t *)(((__u8 *) header) + header->e_phoff))[i], header, as);
  94.         if (rc != EE_OK)
  95.             return rc;
  96.     }
  97.  
  98.     /* Inspect all section headers and proccess them. */
  99.     for (i = 0; i < header->e_shnum; i++) {
  100.         rc = section_header(&((elf_section_header_t *)(((__u8 *) header) + header->e_shoff))[i], header, as);
  101.         if (rc != EE_OK)
  102.             return rc;
  103.     }
  104.  
  105.     return EE_OK;
  106. }
  107.  
  108. /** Print error message according to error code.
  109.  *
  110.  * @param rc Return code returned by elf_load().
  111.  *
  112.  * @return NULL terminated description of error.
  113.  */
  114. char *elf_error(int rc)
  115. {
  116.     ASSERT(rc < sizeof(error_codes)/sizeof(char *));
  117.  
  118.     return error_codes[rc];
  119. }
  120.  
  121. /** Process segment header.
  122.  *
  123.  * @param entry Segment header.
  124.  * @param elf ELF header.
  125.  * @param as Address space into wich the ELF is being loaded.
  126.  *
  127.  * @return EE_OK on success, error code otherwise.
  128.  */
  129. static int segment_header(elf_segment_header_t *entry, elf_header_t *elf, as_t *as)
  130. {
  131.     switch (entry->p_type) {
  132.         case PT_NULL:
  133.         case PT_PHDR:
  134.         break;
  135.         case PT_LOAD:
  136.         return load_segment(entry, elf, as);
  137.         break;
  138.         case PT_DYNAMIC:
  139.         case PT_INTERP:
  140.         case PT_SHLIB:
  141.         case PT_NOTE:
  142.         case PT_LOPROC:
  143.         case PT_HIPROC:
  144.         default:
  145.         return EE_UNSUPPORTED;
  146.         break;
  147.     }
  148.     return EE_OK;
  149. }
  150.  
  151. /** Load segment described by program header entry.
  152.  *
  153.  * @param entry Program header entry describing segment to be loaded.
  154.  * @param elf ELF header.
  155.  * @param as Address space into wich the ELF is being loaded.
  156.  *
  157.  * @return EE_OK on success, error code otherwise.
  158.  */
  159. int load_segment(elf_segment_header_t *entry, elf_header_t *elf, as_t *as)
  160. {
  161.     as_area_t *a;
  162.     int i, flags = 0;
  163.     size_t segment_size;
  164.     __u8 *segment;
  165.  
  166.     if (entry->p_align > 1) {
  167.         if ((entry->p_offset % entry->p_align) != (entry->p_vaddr % entry->p_align)) {
  168.             return EE_INVALID;
  169.         }
  170.     }
  171.  
  172.     if (entry->p_flags & PF_X)
  173.         flags |= AS_AREA_EXEC;
  174.     if (entry->p_flags & PF_W)
  175.         flags |= AS_AREA_WRITE;
  176.     if (entry->p_flags & PF_R)
  177.         flags |= AS_AREA_READ;
  178.  
  179.     /*
  180.      * Check if the virtual address starts on page boundary.
  181.      */
  182.     if (ALIGN_UP(entry->p_vaddr, PAGE_SIZE) != entry->p_vaddr)
  183.         return EE_UNSUPPORTED;
  184.  
  185.     segment_size = ALIGN_UP(max(entry->p_filesz, entry->p_memsz), PAGE_SIZE);
  186.     if ((entry->p_flags & PF_W)) {
  187.         /* If writable, copy data (should be COW in the future) */
  188.         segment = malloc(segment_size, 0);
  189.         memsetb((__address) (segment + entry->p_filesz), segment_size - entry->p_filesz, 0);
  190.         memcpy(segment, (void *) (((__address) elf) + entry->p_offset), entry->p_filesz);
  191.     } else /* Map identically original data */
  192.         segment = ((void *) elf) + entry->p_offset;
  193.  
  194.     a = as_area_create(as, flags, entry->p_memsz, entry->p_vaddr, AS_AREA_ATTR_NONE);
  195.     if (!a)
  196.         return EE_MEMORY;
  197.    
  198.     for (i = 0; i < SIZE2FRAMES(entry->p_filesz); i++) {
  199.         as_set_mapping(as, entry->p_vaddr + i*PAGE_SIZE, KA2PA(((__address) segment) + i*PAGE_SIZE));
  200.     }
  201.    
  202.     return EE_OK;
  203. }
  204.  
  205. /** Process section header.
  206.  *
  207.  * @param entry Segment header.
  208.  * @param elf ELF header.
  209.  * @param as Address space into wich the ELF is being loaded.
  210.  *
  211.  * @return EE_OK on success, error code otherwise.
  212.  */
  213. static int section_header(elf_section_header_t *entry, elf_header_t *elf, as_t *as)
  214. {
  215.     switch (entry->sh_type) {
  216.         default:
  217.         break;
  218.     }
  219.    
  220.     return EE_OK;
  221. }
  222.