paludis Version 3.0.2
The one true package mangler
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/iterator_range.hh>
27#include <paludis/util/pimp.hh>
28#include <paludis/util/wrapped_forward_iterator-fwd.hh>
29#include <iosfwd>
30
31#include <elf.h>
32
33namespace paludis
34{
35 class InvalidElfFileError :
37 {
38 public:
39 InvalidElfFileError(const InvalidElfFileError &) = default;
40 InvalidElfFileError(const std::string &) noexcept;
41 };
42
43 template <typename ElfType_>
45
46 template <typename ElfType_>
47 class PALUDIS_VISIBLE ElfObject
48 {
49 private:
50 Pimp<ElfObject> _imp;
51
52 typename ElfType_::Header _hdr;
53
54 public:
55 static bool is_valid_elf(std::istream & stream);
56 ElfObject(std::istream & stream);
57 ~ElfObject();
58
59 /**
60 * Returns e_type from the ELF header
61 */
62 unsigned int get_type() const
63 {
64 return _hdr.e_type;
65 }
66
67 /**
68 * Returns e_machine from the ELF header
69 */
70 unsigned int get_arch() const
71 {
72 return _hdr.e_machine;
73 }
74
75 /**
76 * Returns the OS ABI field from the ident field
77 */
78 unsigned char get_os_abi() const
79 {
80 return _hdr.e_ident[EI_OSABI];
81 }
82
83 /**
84 * Returns the OS ABI Version field from the ident field
85 */
86 unsigned char get_os_abi_version() const
87 {
88 return _hdr.e_ident[EI_ABIVERSION];
89 }
90
91 /**
92 * Returns the processor-specific flags
93 */
94 unsigned int get_flags() const
95 {
96 return _hdr.e_flags;
97 }
98
99 /**
100 * Returns whether this ELF file uses big-endian or little-endian
101 * Please note: If you didn't use is_valid_elf(...) to check whether
102 * this is an ELF object, this might be wrong
103 */
104 unsigned int is_big_endian() const
105 {
106 // We already checked in is_valid_elf_type whether it's valid or not
107 return (_hdr.e_ident[EI_DATA] == ELFDATA2MSB);
108 }
109
110 unsigned int get_number_of_sections() const
111 {
112 return _hdr.e_shnum;
113 }
114
115 typedef ElfObjectSectionIteratorTag<ElfType_> SectionIteratorTag;
117 SectionIterator section_begin() const;
118 SectionIterator section_end() const;
119 IteratorRange<SectionIterator> sections() const noexcept
120 {
121 return {section_begin(), section_end()};
122 }
123
124 SectionIterator get_section_by_index(unsigned int index) const;
125
126 void resolve_all_strings();
127 };
128}
129
130#endif
131
unsigned int get_type() const
Definition elf.hh:62
unsigned char get_os_abi_version() const
Definition elf.hh:86
unsigned int is_big_endian() const
Definition elf.hh:104
unsigned int get_flags() const
Definition elf.hh:94
unsigned int get_arch() const
Definition elf.hh:70
unsigned char get_os_abi() const
Definition elf.hh:78
Definition exception.hh:77
Definition pimp.hh:52
Definition wrapped_forward_iterator.hh:42
#define PALUDIS_VISIBLE
Definition attributes.hh:59
Definition about_metadata-fwd.hh:24