COFFI
1.2
               
coffi_relocation.hpp
Go to the documentation of this file.
1 /*
2 Copyright (C) 2014-2014 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 /*! @file coffi_relocation.hpp
24  * @brief COFFI library classes for the COFF relocation entries.
25  *
26  * Do not include this file directly. This file is included by coffi.hpp.
27  */
28 
29 #ifndef COFFI_RELOCATION_HPP
30 #define COFFI_RELOCATION_HPP
31 
32 #include <string>
33 #include <iostream>
34 
35 #include <coffi/coffi_utils.hpp>
36 #include <coffi/coffi_symbols.hpp>
37 
38 namespace COFFI {
39 
40 //------------------------------------------------------------------------------
41 //! Class for accessing a COFF section relocation entry.
43 {
44  public:
45  //------------------------------------------------------------------------------
47  const symbol_provider* sym,
48  const architecture_provider* arch)
49  : stn_{stn}, sym_{sym}, arch_{arch}
50  {
51  }
52 
53  //! @accessors{relocation}
54  COFFI_GET_SET_ACCESS(uint32_t, virtual_address);
55  COFFI_GET_ACCESS(uint32_t, symbol_table_index);
56  COFFI_GET_SET_ACCESS(uint32_t, type);
57  COFFI_GET_SET_ACCESS(uint16_t, reserved);
58  //! @endaccessors
59 
60  //------------------------------------------------------------------------------
61  const std::string& get_symbol() const { return symbol_name; }
62 
63  //------------------------------------------------------------------------------
64  void set_symbol(uint32_t symbol_table_index)
65  {
66  header.symbol_table_index = symbol_table_index;
67  const symbol* sym = sym_->get_symbol(header.symbol_table_index);
68  symbol_name = "";
69  if (sym) {
70  symbol_name = sym->get_name();
71  }
72  }
73 
74  //------------------------------------------------------------------------------
75  void load(std::istream& stream)
76  {
77  std::fill_n(reinterpret_cast<char*>(&header), sizeof(header), '\0');
78 
79  switch (arch_->get_architecture()) {
81  {
82  rel_entry_ti h;
83  stream.read((char*)&(h), sizeof(h));
84  header.virtual_address = h.virtual_address;
85  header.symbol_table_index = h.symbol_table_index;
86  header.type = h.type;
87  header.reserved = h.reserved;
88  break;
89  }
91  {
93  stream.read((char*)&(h), sizeof(h));
94  header.virtual_address = h.virtual_address;
95  header.symbol_table_index = h.symbol_table_index;
96  header.type = h.type;
97  break;
98  }
99  default:
100  {
101  rel_entry h;
102  stream.read((char*)&(h), sizeof(h));
103  header.virtual_address = h.virtual_address;
104  header.symbol_table_index = h.symbol_table_index;
105  header.type = h.type;
106  break;
107  }
108  }
109 
110  set_symbol(get_symbol_table_index());
111  }
112 
113  //------------------------------------------------------------------------------
114  void save(std::ostream& stream)
115  {
116  switch (arch_->get_architecture()) {
118  {
119  rel_entry_ti h;
120  h.virtual_address = header.virtual_address;
121  h.symbol_table_index = header.symbol_table_index;
122  h.type = header.type;
123  h.reserved = header.reserved;
124  stream.write((char*)&(h), sizeof(h));
125  break;
126  }
128  {
129  rel_entry_ceva h;
130  h.virtual_address = header.virtual_address;
131  h.symbol_table_index = header.symbol_table_index;
132  h.type = header.type;
133  stream.write((char*)&(h), sizeof(h));
134  break;
135  }
136  default:
137  {
138  rel_entry h;
139  h.virtual_address = header.virtual_address;
140  h.symbol_table_index = header.symbol_table_index;
141  h.type = header.type;
142  stream.write((char*)&(h), sizeof(h));
143  break;
144  }
145  }
146  }
147 
148  //------------------------------------------------------------------------------
149  uint32_t get_sizeof() const
150  {
151  switch (arch_->get_architecture()) {
153  return sizeof(rel_entry_ti);
154  break;
156  return sizeof(rel_entry_ceva);
157  break;
158  default:
159  return sizeof(rel_entry);
160  break;
161  }
162  }
163 
164  //------------------------------------------------------------------------------
165  protected:
166  const string_to_name_provider* stn_;
167  const symbol_provider* sym_;
168  const architecture_provider* arch_;
169  std::string symbol_name;
170  rel_entry_generic header{};
171 };
172 
173 } // namespace COFFI
174 
175 #endif // COFFI_RELOCATION_HPP
Interface for architecture information.
virtual coffi_architecture_t get_architecture() const =0
Returns the coffi object architecture.
Class for accessing a COFF section relocation entry.
Interface for accessing to the string table.
Interface for accessing to the symbol table.
virtual const symbol * get_symbol(uint32_t index) const =0
Gets a symbol from its index.
Class for accessing a COFF symbol.
COFFI library classes for the COFF symbols and symbol table.
COFFI library utilities.
COFFI library namespace.
Definition: coffi.hpp:66
@ COFFI_ARCHITECTURE_CEVA
CEVA Inc.
@ COFFI_ARCHITECTURE_TI
Texas Instruments.
CEVA relocation entry.
Structure capable of storing all the architecture-specific relocation entry structures.
Texas Instruments relocation entry.
PE relocation entry.