genode/repos/base/src/include/base/internal/elf.h
Norman Feske eba9c15746 Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:

* A class with virtual functions can no longer publicly inherit base
  classed without a vtable. The inherited object may either be moved
  to a member variable, or inherited privately. The latter would be
  used for classes that inherit 'List::Element' or 'Avl_node'. In order
  to enable the 'List' and 'Avl_tree' to access the meta data, the
  'List' must become a friend.

* Instead of adding a virtual destructor to abstract base classes,
  we inherit the new 'Interface' class, which contains a virtual
  destructor. This way, single-line abstract base classes can stay
  as compact as they are now. The 'Interface' utility resides in
  base/include/util/interface.h.

* With the new warnings enabled, all member variables must be explicitly
  initialized. Basic types may be initialized with '='. All other types
  are initialized with braces '{ ... }' or as class initializers. If
  basic types and non-basic types appear in a row, it is nice to only
  use the brace syntax (also for basic types) and align the braces.

* If a class contains pointers as members, it must now also provide a
  copy constructor and assignment operator. In the most cases, one
  would make them private, effectively disallowing the objects to be
  copied. Unfortunately, this warning cannot be fixed be inheriting
  our existing 'Noncopyable' class (the compiler fails to detect that
  the inheriting class cannot be copied and still gives the error).
  For now, we have to manually add declarations for both the copy
  constructor and assignment operator as private class members. Those
  declarations should be prepended with a comment like this:

        /*
         * Noncopyable
         */
        Thread(Thread const &);
        Thread &operator = (Thread const &);

  In the future, we should revisit these places and try to replace
  the pointers with references. In the presence of at least one
  reference member, the compiler would no longer implicitly generate
  a copy constructor. So we could remove the manual declaration.

Issue #465
2018-01-17 12:14:35 +01:00

160 lines
3.1 KiB
C++

/*
* \brief ELF binary utility
* \author Christian Helmuth
* \date 2006-05-04
*/
/*
* Copyright (C) 2006-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#ifndef _INCLUDE__BASE__ELF_H_
#define _INCLUDE__BASE__ELF_H_
#include <base/stdint.h>
namespace Genode {
class Elf_segment;
class Elf_binary;
}
class Genode::Elf_binary
{
public:
/**
* Default constructor creates invalid object
*/
Elf_binary() { }
/**
* Constructor
*
* The object is only useful if valid() returns true.
*/
explicit Elf_binary(addr_t start);
/* special types */
struct Flags {
unsigned r:1;
unsigned w:1;
unsigned x:1;
unsigned skip:1;
};
/**
* Read information about program segments
*
* \return properties of the specified program segment
*/
Elf_segment get_segment(unsigned num);
/**
* Check validity
*/
bool valid() { return _valid; }
/**
* Check for dynamic elf
*/
bool dynamically_linked() { return (_dynamic && _interp); }
/************************
** Accessor functions **
************************/
addr_t entry() { return valid() ? _entry : 0; }
private:
/* validity indicator indicates if the loaded ELF is valid and supported */
bool _valid { false };
/* dynamically linked */
bool _dynamic { false };
/* dynamic linker name matches 'genode' */
bool _interp { false };
/* ELF start pointer in memory */
addr_t _start { 0 };
/* ELF entry point */
addr_t _entry { 0 };
/* program segments */
addr_t _ph_table { 0 };
size_t _phentsize { 0 };
unsigned _phnum { 0 };
/************
** Helper **
************/
/**
* Check ELF header compatibility
*/
int _ehdr_check_compat();
/**
* Check program header compatibility
*/
int _ph_table_check_compat();
/**
* Check for dynamic program segments
*/
bool _dynamic_check_compat(unsigned type);
};
class Genode::Elf_segment
{
public:
/**
* Standard constructor creates invalid object
*/
Elf_segment() { }
Elf_segment(const Elf_binary *elf, void *start, size_t file_offset,
size_t file_size, size_t mem_size, Elf_binary::Flags flags)
: _elf(elf), _start((unsigned char *)start), _file_offset(file_offset),
_file_size(file_size), _mem_size(mem_size), _flags(flags)
{
_valid = elf ? true : false;
}
Elf_binary const * elf() { return _elf; }
void * start() { return (void *)_start; }
size_t file_offset() { return _file_offset; }
size_t file_size() { return _file_size; }
size_t mem_size() { return _mem_size; }
Elf_binary::Flags flags() { return _flags; }
/**
* Check validity
*/
bool valid() { return _valid; }
private:
Elf_binary const *_elf { nullptr };
bool _valid { false };
unsigned char *_start { nullptr };
size_t _file_offset { 0 };
size_t _file_size { 0 };
size_t _mem_size { 0 };
Elf_binary::Flags _flags { };
};
#endif /* _INCLUDE__BASE__ELF_H_ */