HIP: Heterogenous-computing Interface for Portability
elfio_section.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 ELFIO_SECTION_HPP
24 #define ELFIO_SECTION_HPP
25 
26 #include <string>
27 #include <iostream>
28 
29 namespace ELFIO {
30 
31 class section {
32  friend class elfio;
33 
34  public:
35  virtual ~section(){};
36 
37  ELFIO_GET_ACCESS_DECL(Elf_Half, index);
38  ELFIO_GET_SET_ACCESS_DECL(std::string, name);
39  ELFIO_GET_SET_ACCESS_DECL(Elf_Word, type);
40  ELFIO_GET_SET_ACCESS_DECL(Elf_Xword, flags);
41  ELFIO_GET_SET_ACCESS_DECL(Elf_Word, info);
42  ELFIO_GET_SET_ACCESS_DECL(Elf_Word, link);
43  ELFIO_GET_SET_ACCESS_DECL(Elf_Xword, addr_align);
44  ELFIO_GET_SET_ACCESS_DECL(Elf_Xword, entry_size);
45  ELFIO_GET_SET_ACCESS_DECL(Elf64_Addr, address);
46  ELFIO_GET_SET_ACCESS_DECL(Elf_Xword, size);
47  ELFIO_GET_SET_ACCESS_DECL(Elf_Word, name_string_offset);
48 
49  virtual const char* get_data() const = 0;
50  virtual void set_data(const char* pData, Elf_Word size) = 0;
51  virtual void set_data(const std::string& data) = 0;
52  virtual void append_data(const char* pData, Elf_Word size) = 0;
53  virtual void append_data(const std::string& data) = 0;
54 
55  protected:
56  ELFIO_GET_SET_ACCESS_DECL(Elf64_Off, offset);
57  ELFIO_SET_ACCESS_DECL(Elf_Half, index);
58 
59  virtual void load(std::istream& f, std::streampos header_offset) = 0;
60  virtual void save(std::ostream& f, std::streampos header_offset,
61  std::streampos data_offset) = 0;
62  virtual bool is_address_initialized() const = 0;
63 };
64 
65 
66 template <class T>
67 class section_impl : public section {
68  public:
69  //------------------------------------------------------------------------------
70  section_impl(const endianess_convertor* convertor_) : convertor(convertor_) {
71  std::fill_n(reinterpret_cast<char*>(&header), sizeof(header), '\0');
72  is_address_set = false;
73  data = 0;
74  data_size = 0;
75  }
76 
77  //------------------------------------------------------------------------------
78  ~section_impl() { delete[] data; }
79 
80  //------------------------------------------------------------------------------
81  // Section info functions
82  ELFIO_GET_SET_ACCESS(Elf_Word, type, header.sh_type);
83  ELFIO_GET_SET_ACCESS(Elf_Xword, flags, header.sh_flags);
84  ELFIO_GET_SET_ACCESS(Elf_Xword, size, header.sh_size);
85  ELFIO_GET_SET_ACCESS(Elf_Word, link, header.sh_link);
86  ELFIO_GET_SET_ACCESS(Elf_Word, info, header.sh_info);
87  ELFIO_GET_SET_ACCESS(Elf_Xword, addr_align, header.sh_addralign);
88  ELFIO_GET_SET_ACCESS(Elf_Xword, entry_size, header.sh_entsize);
89  ELFIO_GET_SET_ACCESS(Elf_Word, name_string_offset, header.sh_name);
90  ELFIO_GET_ACCESS(Elf64_Addr, address, header.sh_addr);
91 
92  //------------------------------------------------------------------------------
93  Elf_Half get_index() const { return index; }
94 
95 
96  //------------------------------------------------------------------------------
97  std::string get_name() const { return name; }
98 
99  //------------------------------------------------------------------------------
100  void set_name(std::string name_) { name = name_; }
101 
102  //------------------------------------------------------------------------------
103  void set_address(Elf64_Addr value) {
104  header.sh_addr = value;
105  header.sh_addr = (*convertor)(header.sh_addr);
106  is_address_set = true;
107  }
108 
109  //------------------------------------------------------------------------------
110  bool is_address_initialized() const { return is_address_set; }
111 
112  //------------------------------------------------------------------------------
113  const char* get_data() const { return data; }
114 
115  //------------------------------------------------------------------------------
116  void set_data(const char* raw_data, Elf_Word size) {
117  if (get_type() != SHT_NOBITS) {
118  delete[] data;
119  try {
120  data = new char[size];
121  } catch (const std::bad_alloc&) {
122  data = 0;
123  data_size = 0;
124  size = 0;
125  }
126  if (0 != data && 0 != raw_data) {
127  data_size = size;
128  std::copy(raw_data, raw_data + size, data);
129  }
130  }
131 
132  set_size(size);
133  }
134 
135  //------------------------------------------------------------------------------
136  void set_data(const std::string& str_data) {
137  return set_data(str_data.c_str(), (Elf_Word)str_data.size());
138  }
139 
140  //------------------------------------------------------------------------------
141  void append_data(const char* raw_data, Elf_Word size) {
142  if (get_type() != SHT_NOBITS) {
143  if (get_size() + size < data_size) {
144  std::copy(raw_data, raw_data + size, data + get_size());
145  } else {
146  data_size = 2 * (data_size + size);
147  char* new_data;
148  try {
149  new_data = new char[data_size];
150  } catch (const std::bad_alloc&) {
151  new_data = 0;
152  size = 0;
153  }
154  if (0 != new_data) {
155  std::copy(data, data + get_size(), new_data);
156  std::copy(raw_data, raw_data + size, new_data + get_size());
157  delete[] data;
158  data = new_data;
159  }
160  }
161  set_size(get_size() + size);
162  }
163  }
164 
165  //------------------------------------------------------------------------------
166  void append_data(const std::string& str_data) {
167  return append_data(str_data.c_str(), (Elf_Word)str_data.size());
168  }
169 
170  //------------------------------------------------------------------------------
171  protected:
172  //------------------------------------------------------------------------------
173  ELFIO_GET_SET_ACCESS(Elf64_Off, offset, header.sh_offset);
174 
175  //------------------------------------------------------------------------------
176  void set_index(Elf_Half value) { index = value; }
177 
178  //------------------------------------------------------------------------------
179  void load(std::istream& stream, std::streampos header_offset) {
180  std::fill_n(reinterpret_cast<char*>(&header), sizeof(header), '\0');
181  stream.seekg(header_offset);
182  stream.read(reinterpret_cast<char*>(&header), sizeof(header));
183 
184  Elf_Xword size = get_size();
185  if (0 == data && SHT_NULL != get_type() && SHT_NOBITS != get_type()) {
186  try {
187  data = new char[size];
188  } catch (const std::bad_alloc&) {
189  data = 0;
190  data_size = 0;
191  }
192  if (0 != size) {
193  stream.seekg((*convertor)(header.sh_offset));
194  stream.read(data, size);
195  data_size = size;
196  }
197  }
198  }
199 
200  //------------------------------------------------------------------------------
201  void save(std::ostream& f, std::streampos header_offset, std::streampos data_offset) {
202  if (0 != get_index()) {
203  header.sh_offset = data_offset;
204  header.sh_offset = (*convertor)(header.sh_offset);
205  }
206 
207  save_header(f, header_offset);
208  if (get_type() != SHT_NOBITS && get_type() != SHT_NULL && get_size() != 0 && data != 0) {
209  save_data(f, data_offset);
210  }
211  }
212 
213  //------------------------------------------------------------------------------
214  private:
215  //------------------------------------------------------------------------------
216  void save_header(std::ostream& f, std::streampos header_offset) const {
217  f.seekp(header_offset);
218  f.write(reinterpret_cast<const char*>(&header), sizeof(header));
219  }
220 
221  //------------------------------------------------------------------------------
222  void save_data(std::ostream& f, std::streampos data_offset) const {
223  f.seekp(data_offset);
224  f.write(get_data(), get_size());
225  }
226 
227  //------------------------------------------------------------------------------
228  private:
229  T header;
230  Elf_Half index;
231  std::string name;
232  char* data;
233  Elf_Word data_size;
234  const endianess_convertor* convertor;
235  bool is_address_set;
236 };
237 
238 } // namespace ELFIO
239 
240 #endif // ELFIO_SECTION_HPP
ELFIO::endianess_convertor
Definition: elfio_utils.hpp:51
ELFIO::section_impl
Definition: elfio_section.hpp:67
ELFIO::elfio
Definition: elfio.hpp:59
ELFIO::section
Definition: elfio_section.hpp:31