HIP: Heterogenous-computing Interface for Portability
elfio_segment.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_SEGMENT_HPP
24 #define ELFIO_SEGMENT_HPP
25 
26 #include <iostream>
27 #include <vector>
28 
29 namespace ELFIO {
30 
31 class segment {
32  friend class elfio;
33 
34  public:
35  virtual ~segment(){};
36 
37  ELFIO_GET_ACCESS_DECL(Elf_Half, index);
38  ELFIO_GET_SET_ACCESS_DECL(Elf_Word, type);
39  ELFIO_GET_SET_ACCESS_DECL(Elf_Word, flags);
40  ELFIO_GET_SET_ACCESS_DECL(Elf_Xword, align);
41  ELFIO_GET_SET_ACCESS_DECL(Elf64_Addr, virtual_address);
42  ELFIO_GET_SET_ACCESS_DECL(Elf64_Addr, physical_address);
43  ELFIO_GET_SET_ACCESS_DECL(Elf_Xword, file_size);
44  ELFIO_GET_SET_ACCESS_DECL(Elf_Xword, memory_size);
45  ELFIO_GET_ACCESS_DECL(Elf64_Off, offset);
46 
47  virtual const char* get_data() const = 0;
48 
49  virtual Elf_Half add_section_index(Elf_Half index, Elf_Xword addr_align) = 0;
50  virtual Elf_Half get_sections_num() const = 0;
51  virtual Elf_Half get_section_index_at(Elf_Half num) const = 0;
52  virtual bool is_offset_initialized() const = 0;
53 
54  protected:
55  ELFIO_SET_ACCESS_DECL(Elf64_Off, offset);
56  ELFIO_SET_ACCESS_DECL(Elf_Half, index);
57 
58  virtual const std::vector<Elf_Half>& get_sections() const = 0;
59  virtual void load(std::istream& stream, std::streampos header_offset) = 0;
60  virtual void save(std::ostream& f, std::streampos header_offset,
61  std::streampos data_offset) = 0;
62 };
63 
64 
65 //------------------------------------------------------------------------------
66 template <class T>
67 class segment_impl : public segment {
68  public:
69  //------------------------------------------------------------------------------
70  segment_impl(endianess_convertor* convertor_) : convertor(convertor_) {
71  is_offset_set = false;
72  std::fill_n(reinterpret_cast<char*>(&ph), sizeof(ph), '\0');
73  data = 0;
74  }
75 
76  //------------------------------------------------------------------------------
77  virtual ~segment_impl() { delete[] data; }
78 
79  //------------------------------------------------------------------------------
80  // Section info functions
81  ELFIO_GET_SET_ACCESS(Elf_Word, type, ph.p_type);
82  ELFIO_GET_SET_ACCESS(Elf_Word, flags, ph.p_flags);
83  ELFIO_GET_SET_ACCESS(Elf_Xword, align, ph.p_align);
84  ELFIO_GET_SET_ACCESS(Elf64_Addr, virtual_address, ph.p_vaddr);
85  ELFIO_GET_SET_ACCESS(Elf64_Addr, physical_address, ph.p_paddr);
86  ELFIO_GET_SET_ACCESS(Elf_Xword, file_size, ph.p_filesz);
87  ELFIO_GET_SET_ACCESS(Elf_Xword, memory_size, ph.p_memsz);
88  ELFIO_GET_ACCESS(Elf64_Off, offset, ph.p_offset);
89 
90  //------------------------------------------------------------------------------
91  Elf_Half get_index() const { return index; }
92 
93  //------------------------------------------------------------------------------
94  const char* get_data() const { return data; }
95 
96  //------------------------------------------------------------------------------
97  Elf_Half add_section_index(Elf_Half sec_index, Elf_Xword addr_align) {
98  sections.push_back(sec_index);
99  if (addr_align > get_align()) {
100  set_align(addr_align);
101  }
102 
103  return (Elf_Half)sections.size();
104  }
105 
106  //------------------------------------------------------------------------------
107  Elf_Half get_sections_num() const { return (Elf_Half)sections.size(); }
108 
109  //------------------------------------------------------------------------------
110  Elf_Half get_section_index_at(Elf_Half num) const {
111  if (num < sections.size()) {
112  return sections[num];
113  }
114 
115  return -1;
116  }
117 
118  //------------------------------------------------------------------------------
119  protected:
120  //------------------------------------------------------------------------------
121 
122  //------------------------------------------------------------------------------
123  void set_offset(Elf64_Off value) {
124  ph.p_offset = value;
125  ph.p_offset = (*convertor)(ph.p_offset);
126  is_offset_set = true;
127  }
128 
129  //------------------------------------------------------------------------------
130  bool is_offset_initialized() const { return is_offset_set; }
131 
132  //------------------------------------------------------------------------------
133  const std::vector<Elf_Half>& get_sections() const { return sections; }
134 
135  //------------------------------------------------------------------------------
136  void set_index(Elf_Half value) { index = value; }
137 
138  //------------------------------------------------------------------------------
139  void load(std::istream& stream, std::streampos header_offset) {
140  stream.seekg(header_offset);
141  stream.read(reinterpret_cast<char*>(&ph), sizeof(ph));
142  is_offset_set = true;
143 
144  if (PT_NULL != get_type() && 0 != get_file_size()) {
145  stream.seekg((*convertor)(ph.p_offset));
146  Elf_Xword size = get_file_size();
147  try {
148  data = new char[size];
149  } catch (const std::bad_alloc&) {
150  data = 0;
151  }
152  if (0 != data) {
153  stream.read(data, size);
154  }
155  }
156  }
157 
158  //------------------------------------------------------------------------------
159  void save(std::ostream& f, std::streampos header_offset, std::streampos data_offset) {
160  ph.p_offset = data_offset;
161  ph.p_offset = (*convertor)(ph.p_offset);
162  f.seekp(header_offset);
163  f.write(reinterpret_cast<const char*>(&ph), sizeof(ph));
164  }
165 
166  //------------------------------------------------------------------------------
167  private:
168  T ph;
169  Elf_Half index;
170  char* data;
171  std::vector<Elf_Half> sections;
172  endianess_convertor* convertor;
173  bool is_offset_set;
174 };
175 
176 } // namespace ELFIO
177 
178 #endif // ELFIO_SEGMENT_HPP
ELFIO::endianess_convertor
Definition: elfio_utils.hpp:51
ELFIO::segment_impl
Definition: elfio_segment.hpp:67
ELFIO::elfio
Definition: elfio.hpp:59
ELFIO::segment
Definition: elfio_segment.hpp:31