HIP: Heterogenous-computing Interface for Portability
elfio_header.hpp
1 /*
2 Copyright (C) 2001-2015 by Serge Lamikhov-Center
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 
23 #ifndef ELF_HEADER_HPP
24 #define ELF_HEADER_HPP
25 
26 #include <iostream>
27 
28 namespace ELFIO {
29 
30 class elf_header {
31  public:
32  virtual ~elf_header(){};
33  virtual bool load(std::istream& stream) = 0;
34  virtual bool save(std::ostream& stream) const = 0;
35 
36  // ELF header functions
37  ELFIO_GET_ACCESS_DECL(unsigned char, class);
38  ELFIO_GET_ACCESS_DECL(unsigned char, elf_version);
39  ELFIO_GET_ACCESS_DECL(unsigned char, encoding);
40  ELFIO_GET_ACCESS_DECL(Elf_Word, version);
41  ELFIO_GET_ACCESS_DECL(Elf_Half, header_size);
42  ELFIO_GET_ACCESS_DECL(Elf_Half, section_entry_size);
43  ELFIO_GET_ACCESS_DECL(Elf_Half, segment_entry_size);
44 
45  ELFIO_GET_SET_ACCESS_DECL(unsigned char, os_abi);
46  ELFIO_GET_SET_ACCESS_DECL(unsigned char, abi_version);
47  ELFIO_GET_SET_ACCESS_DECL(Elf_Half, type);
48  ELFIO_GET_SET_ACCESS_DECL(Elf_Half, machine);
49  ELFIO_GET_SET_ACCESS_DECL(Elf_Word, flags);
50  ELFIO_GET_SET_ACCESS_DECL(Elf64_Addr, entry);
51  ELFIO_GET_SET_ACCESS_DECL(Elf_Half, sections_num);
52  ELFIO_GET_SET_ACCESS_DECL(Elf64_Off, sections_offset);
53  ELFIO_GET_SET_ACCESS_DECL(Elf_Half, segments_num);
54  ELFIO_GET_SET_ACCESS_DECL(Elf64_Off, segments_offset);
55  ELFIO_GET_SET_ACCESS_DECL(Elf_Half, section_name_str_index);
56 };
57 
58 
59 template <class T>
61 template <>
63  typedef Elf32_Phdr Phdr_type;
64  typedef Elf32_Shdr Shdr_type;
65  static const unsigned char file_class = ELFCLASS32;
66 };
67 template <>
69  typedef Elf64_Phdr Phdr_type;
70  typedef Elf64_Shdr Shdr_type;
71  static const unsigned char file_class = ELFCLASS64;
72 };
73 
74 template <class T>
75 class elf_header_impl : public elf_header {
76  public:
77  elf_header_impl(endianess_convertor* convertor_, unsigned char encoding) {
78  convertor = convertor_;
79 
80  std::fill_n(reinterpret_cast<char*>(&header), sizeof(header), '\0');
81 
82  header.e_ident[EI_MAG0] = ELFMAG0;
83  header.e_ident[EI_MAG1] = ELFMAG1;
84  header.e_ident[EI_MAG2] = ELFMAG2;
85  header.e_ident[EI_MAG3] = ELFMAG3;
86  header.e_ident[EI_CLASS] = elf_header_impl_types<T>::file_class;
87  header.e_ident[EI_DATA] = encoding;
88  header.e_ident[EI_VERSION] = EV_CURRENT;
89  header.e_version = EV_CURRENT;
90  header.e_version = (*convertor)(header.e_version);
91  header.e_ehsize = (sizeof(header));
92  header.e_ehsize = (*convertor)(header.e_ehsize);
93  header.e_shstrndx = (*convertor)((Elf_Half)1);
94  header.e_phentsize = sizeof(typename elf_header_impl_types<T>::Phdr_type);
95  header.e_shentsize = sizeof(typename elf_header_impl_types<T>::Shdr_type);
96  header.e_phentsize = (*convertor)(header.e_phentsize);
97  header.e_shentsize = (*convertor)(header.e_shentsize);
98  }
99 
100  bool load(std::istream& stream) {
101  stream.seekg(0);
102  stream.read(reinterpret_cast<char*>(&header), sizeof(header));
103 
104  return (stream.gcount() == sizeof(header));
105  }
106 
107  bool save(std::ostream& stream) const {
108  stream.seekp(0);
109  stream.write(reinterpret_cast<const char*>(&header), sizeof(header));
110 
111  return stream.good();
112  }
113 
114  // ELF header functions
115  ELFIO_GET_ACCESS(unsigned char, class, header.e_ident[EI_CLASS]);
116  ELFIO_GET_ACCESS(unsigned char, elf_version, header.e_ident[EI_VERSION]);
117  ELFIO_GET_ACCESS(unsigned char, encoding, header.e_ident[EI_DATA]);
118  ELFIO_GET_ACCESS(Elf_Word, version, header.e_version);
119  ELFIO_GET_ACCESS(Elf_Half, header_size, header.e_ehsize);
120  ELFIO_GET_ACCESS(Elf_Half, section_entry_size, header.e_shentsize);
121  ELFIO_GET_ACCESS(Elf_Half, segment_entry_size, header.e_phentsize);
122 
123  ELFIO_GET_SET_ACCESS(unsigned char, os_abi, header.e_ident[EI_OSABI]);
124  ELFIO_GET_SET_ACCESS(unsigned char, abi_version, header.e_ident[EI_ABIVERSION]);
125  ELFIO_GET_SET_ACCESS(Elf_Half, type, header.e_type);
126  ELFIO_GET_SET_ACCESS(Elf_Half, machine, header.e_machine);
127  ELFIO_GET_SET_ACCESS(Elf_Word, flags, header.e_flags);
128  ELFIO_GET_SET_ACCESS(Elf_Half, section_name_str_index, header.e_shstrndx);
129  ELFIO_GET_SET_ACCESS(Elf64_Addr, entry, header.e_entry);
130  ELFIO_GET_SET_ACCESS(Elf_Half, sections_num, header.e_shnum);
131  ELFIO_GET_SET_ACCESS(Elf64_Off, sections_offset, header.e_shoff);
132  ELFIO_GET_SET_ACCESS(Elf_Half, segments_num, header.e_phnum);
133  ELFIO_GET_SET_ACCESS(Elf64_Off, segments_offset, header.e_phoff);
134 
135  private:
136  T header;
137  endianess_convertor* convertor;
138 };
139 
140 } // namespace ELFIO
141 
142 #endif // ELF_HEADER_HPP
ELFIO::Elf64_Phdr
Definition: elf_types.hpp:658
ELFIO::Elf64_Shdr
Definition: elf_types.hpp:632
ELFIO::Elf64_Ehdr
Definition: elf_types.hpp:600
ELFIO::elf_header_impl
Definition: elfio_header.hpp:75
ELFIO::endianess_convertor
Definition: elfio_utils.hpp:51
ELFIO::elf_header_impl_types
Definition: elfio_header.hpp:60
ELFIO::elf_header
Definition: elfio_header.hpp:30
ELFIO::Elf32_Phdr
Definition: elf_types.hpp:647
ELFIO::Elf32_Shdr
Definition: elf_types.hpp:619
ELFIO::Elf32_Ehdr
Definition: elf_types.hpp:583