paludis  Version 2.6.0
elf.hh
1 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
2 
3 /*
4  * Copyright (c) 2007 Tiziano Müller
5  * Copyright (c) 2007 David Leverton
6  *
7  * This file is part of the Paludis package manager. Paludis is free software;
8  * you can redistribute it and/or modify it under the terms of the GNU General
9  * Public License version 2, as published by the Free Software Foundation.
10  *
11  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18  * Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_ELF_HH
22 #define PALUDIS_GUARD_PALUDIS_UTIL_ELF_HH 1
23 
24 #include <paludis/util/elf_sections.hh>
26 #include <paludis/util/pimp.hh>
27 #include <paludis/util/wrapped_forward_iterator-fwd.hh>
28 #include <iosfwd>
29 
30 #include <elf.h>
31 
32 namespace paludis
33 {
35  public paludis::Exception
36  {
37  public:
39  InvalidElfFileError(const std::string &) noexcept;
40  };
41 
42  template <typename ElfType_>
44 
45  template <typename ElfType_>
47  {
48  private:
49  Pimp<ElfObject> _imp;
50 
51  typename ElfType_::Header _hdr;
52 
53  public:
54  static bool is_valid_elf(std::istream & stream);
55  ElfObject(std::istream & stream);
56  ~ElfObject();
57 
58  /**
59  * Returns e_type from the ELF header
60  */
61  unsigned int get_type() const
62  {
63  return _hdr.e_type;
64  }
65 
66  /**
67  * Returns e_machine from the ELF header
68  */
69  unsigned int get_arch() const
70  {
71  return _hdr.e_machine;
72  }
73 
74  /**
75  * Returns the OS ABI field from the ident field
76  */
77  unsigned char get_os_abi() const
78  {
79  return _hdr.e_ident[EI_OSABI];
80  }
81 
82  /**
83  * Returns the OS ABI Version field from the ident field
84  */
85  unsigned char get_os_abi_version() const
86  {
87  return _hdr.e_ident[EI_ABIVERSION];
88  }
89 
90  /**
91  * Returns the processor-specific flags
92  */
93  unsigned int get_flags() const
94  {
95  return _hdr.e_flags;
96  }
97 
98  /**
99  * Returns whether this ELF file uses big-endian or little-endian
100  * Please note: If you didn't use is_valid_elf(...) to check whether
101  * this is an ELF object, this might be wrong
102  */
103  unsigned int is_big_endian() const
104  {
105  // We already checked in is_valid_elf_type whether it's valid or not
106  return (_hdr.e_ident[EI_DATA] == ELFDATA2MSB);
107  }
108 
109  unsigned int get_number_of_sections() const
110  {
111  return _hdr.e_shnum;
112  }
113 
116  SectionIterator section_begin() const;
117  SectionIterator section_end() const;
118 
119  SectionIterator get_section_by_index(unsigned int index) const;
120 
121  void resolve_all_strings();
122  };
123 }
124 
125 #endif
126 
Definition: pimp.hh:51
Definition: about_metadata-fwd.hh:23
Definition: elf.hh:46
unsigned int get_arch() const
Definition: elf.hh:69
unsigned char get_os_abi() const
Definition: elf.hh:77
unsigned int is_big_endian() const
Definition: elf.hh:103
Definition: exception.hh:74
Definition: elf.hh:34
unsigned int get_type() const
Definition: elf.hh:61
unsigned char get_os_abi_version() const
Definition: elf.hh:85
Definition: wrapped_forward_iterator-fwd.hh:26
unsigned int get_flags() const
Definition: elf.hh:93
#define PALUDIS_VISIBLE
Definition: attributes.hh:59