1  /*
   2   * \brief  ELF binary utility
   3   * \author Christian Helmuth
   4   * \date   2006-05-04
   5   */

   6  
   7  /*
   8   * Copyright (C) 2006-2013 Genode Labs GmbH
   9   *
  10   * This file is part of the Genode OS framework, which is distributed
  11   * under the terms of the GNU General Public License version 2.
  12   */

  13  
  14  #ifndef _INCLUDE__BASE__ELF_H_
  15  #define _INCLUDE__BASE__ELF_H_
  16  
  17  #include <base/stdint.h>
  18  
  19  namespace Genode {
  20  
  21     class Elf_segment;
  22     class Elf_binary;
  23  }

  24  
  25  
  26  class Genode::Elf_binary
  27  {
  28     public:
  29  
  30        /**
  31         * Default constructor creates invalid object
  32         */

  33        Elf_binary() : _valid(false) { }

  34  
  35        /**
  36         * Constructor
  37         *
  38         * The object is only useful if valid() returns true.
  39         */

  40        explicit Elf_binary(addr_t start);

  41  
  42        /* special types */
  43  
  44        struct Flags {
  45           unsigned r:1;
  46           unsigned w:1;
  47           unsigned x:1;
  48           unsigned skip:1;

  49        }
;

  50  
  51        /**
  52         * Read information about program segments
  53         *
  54         * \return  properties of the specified program segment
  55         */

  56        Elf_segment get_segment(unsigned num);

  57  
  58        /**
  59         * Check validity
  60         */

  61        bool valid() { return _valid; }

  62  
  63        /**
  64         * Check for dynamic elf
  65         */

  66        bool is_dynamically_linked() { return (_dynamic && _interp); }

  67  
  68  
  69        /************************
  70         ** Accessor functions **
  71         ************************/

  72  
  73        addr_t entry() { return valid() ? _entry : 0; }

  74  
  75     private:
  76  
  77        /* validity indicator indicates if the loaded ELF is valid and supported */
  78        bool _valid;
  79  
  80        /* dynamically linked */

  81        bool _dynamic;
  82  
  83        /* dynamic linker name matches `genode` */

  84        bool _interp;
  85  
  86        /* ELF start pointer in memory */

  87        addr_t _start;
  88  
  89        /* ELF entry point */

  90        addr_t _entry;
  91  
  92        /* program segments */

  93        addr_t   _ph_table;
  94        size_t   _phentsize;
  95        unsigned _phnum;
  96  
  97  
  98        /************
  99         ** Helper **
 100         ************/

 101  
 102        /**
 103         * Check ELF header compatibility
 104         */

 105        int _ehdr_check_compat();

 106  
 107        /**
 108         * Check program header compatibility
 109         */

 110        int _ph_table_check_compat();

 111  
 112        /**
 113         * Check for dynamic program segments
 114         */

 115        bool _dynamic_check_compat(unsigned type);

 116  }
;

 117  
 118  
 119  class Genode::Elf_segment
 120  {
 121     public:
 122  
 123        /**
 124         * Standard constructor creates invalid object
 125         */

 126        Elf_segment() : _valid(false) { }

 127  
 128        Elf_segment(const Elf_binary *elf, void *start, size_t file_offset,
 129                    size_t file_size, size_t mem_size, Elf_binary::Flags flags)

 130        : _elf(elf), _start((unsigned char *)start), _file_offset(file_offset),
 131          _file_size(file_size), _mem_size(mem_size), _flags(flags)

 132        {
 133           _valid = elf ? true : false;
 134        }

 135  
 136        const Elf_binary *        elf() { return _elf; }
 137        void *                  start() { return (void *)_start; }
 138        size_t            file_offset() { return _file_offset; }
 139        size_t              file_size() { return _file_size; }
 140        size_t               mem_size() { return _mem_size; }
 141        Elf_binary::Flags       flags() { return _flags; }
 142  
 143        /**
 144         * Check validity
 145         */

 146        bool valid() { return _valid; }

 147  
 148     private:
 149  
 150        const Elf_binary *_elf;
 151        bool              _valid;       /* validity indicator */
 152        unsigned char    *_start;
 153        size_t            _file_offset;
 154        size_t            _file_size;
 155        size_t            _mem_size;
 156        Elf_binary::Flags _flags;

 157  }
;

 158  
 159  #endif /* _INCLUDE__BASE__ELF_H_ */