replicode
segments.h
Go to the documentation of this file.
1 // segments.h
2 //
3 // Author: Eric Nivel
4 //
5 // BSD license:
6 // Copyright (c) 2010, Eric Nivel
7 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 // - Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // - Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // - Neither the name of Eric Nivel nor the
17 // names of their contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
24 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef segments_h
32 #define segments_h
33 
34 #include "r_code/object.h"
35 #include "r_code/list.h"
36 
37 #include "class.h"
38 
39 #include <unordered_map>
40 
41 
42 using namespace r_code;
43 
44 namespace r_comp {
45 
46 class Reference {
47 public:
48  Reference();
49  Reference(const uintptr_t i, const Class &c, const Class &cc);
50  uintptr_t index;
53 };
54 
55 // All classes below map components of r_code::Image into r_comp::Image.
56 // Both images are equivalent, the latter being easier to work with (uses vectors instead of a contiguous structure, that is r_code::Image::data).
57 // All read(word32*,uint64_t)/write(word32*) functions defined in the classes below perfom read/write operations in an r_code::Image::data.
58 
60 private:
61  uintptr_t get_class_array_size();
62  uintptr_t get_classes_size();
63  uintptr_t get_sys_classes_size();
64  size_t get_class_names_size();
65  size_t get_operator_names_size();
66  size_t get_function_names_size();
67 public:
68  Metadata();
69 
70  std::unordered_map<std::string, Class> classes; // non-sys classes, operators and device functions.
71  std::unordered_map<std::string, Class> sys_classes;
72 
73  r_code::vector<std::string> class_names; // classes and sys-classes; does not include set classes.
76  r_code::vector<Class> classes_by_opcodes; // classes indexed by opcodes; used to retrieve member names; registers all classes (incl. set classes).
77 
78  Class *get_class(std::string &class_name);
79  Class *get_class(size_t opcode);
80 
81  void write(uintptr_t *data);
82  void read(uintptr_t* data, size_t size);
83  size_t get_size();
84 };
85 
87 public:
89 
90  void shift(uintptr_t offset);
91 
92  void write(uintptr_t *data);
93  void read(uintptr_t *data, uintptr_t size);
94  size_t get_size() const;
95 };
96 
98 public:
100 
101  ~CodeSegment();
102 
103  void write(uintptr_t *data);
104  void read(uintptr_t *data, size_t object_count);
105  size_t get_size();
106 };
107 
109 public:
110  std::unordered_map<uintptr_t, std::string> symbols; // indexed by objects' OIDs.
111 
112  ~ObjectNames();
113 
114  void write(uintptr_t *data);
115  void read(uintptr_t *data);
116  size_t get_size();
117 };
118 
120 private:
121  size_t map_offset;
122  std::unordered_map<r_code::Code *, size_t> ptrs_to_indices; // used for injection in memory.
123 
124  void add_object(r_code::Code *object);
125  SysObject *add_object(Code *object, std::vector<SysObject *> &imported_objects);
126  size_t get_reference_count(const Code *object) const;
127  void build_references();
128  void build_references(SysObject *sys_object, r_code::Code *object);
129  void unpack_objects(r_code::vector<Code *> &ram_objects);
130 public:
134 
136 
137  Image();
138  ~Image();
139 
140  void add_sys_object(SysObject *object, std::string name); // called by the compiler.
141  void add_sys_object(SysObject *object); // called by add_object().
142 
143  void get_objects(Mem *mem, r_code::vector<r_code::Code *> &ram_objects);
144  template<class O> void get_objects(r_code::vector<Code *> &ram_objects) {
145 
146  for (size_t i = 0; i < code_segment.objects.size(); ++i) {
147 
148  uintptr_t opcode = code_segment.objects[i]->code[0].asOpcode();
149  ram_objects[i] = new O(code_segment.objects[i]);
150  }
151  unpack_objects(ram_objects);
152  }
153 
154  void add_objects(r_code::list<P<r_code::Code> > &objects); // called by the rMem.
155  void add_objects(r_code::list<P<r_code::Code> > &objects, std::vector<SysObject *> &imported_objects); // called by any r_exec code for decompiling on the fly.
156 
157  template<class I> I *serialize() {
158 
159  I *image = (I *)I::Build(timestamp, object_map.get_size(), code_segment.get_size(), object_names.get_size());
160 
161  object_map.shift(image->map_size());
162  object_map.write(image->data());
163  code_segment.write(image->data() + image->map_size());
164  object_names.write(image->data() + image->map_size() + image->code_size());
165 
166  return image;
167  }
168 
169  template<class I> void load(I *image) {
170 
171  timestamp = image->timestamp();
172  object_map.read(image->data(), image->map_size());
173  code_segment.read(image->data() + image->map_size(), image->map_size());
174  object_names.read(image->data() + image->map_size() + image->code_size());
175  }
176 };
177 }
178 
179 
180 #endif
Definition: segments.h:119
std::unordered_map< std::string, Class > sys_classes
Definition: segments.h:71
size_t map_offset
Definition: segments.h:121
void write(uintptr_t *data)
Definition: segments.cpp:307
void get_objects(r_code::vector< Code * > &ram_objects)
Definition: segments.h:144
Definition: class.h:41
size_t get_size() const
Definition: segments.cpp:294
#define dll_export
Definition: dll.h:44
void read(uintptr_t *data, size_t object_count)
Definition: segments.cpp:317
Definition: object.h:328
I * serialize()
Definition: segments.h:157
Definition: atom.cpp:36
void write(uintptr_t *data)
Definition: segments.cpp:349
ObjectMap object_map
Definition: segments.h:131
Definition: segments.h:86
r_code::vector< std::string > function_names
Definition: segments.h:75
void write(uintptr_t *data)
Definition: segments.cpp:282
std::unordered_map< r_code::Code *, size_t > ptrs_to_indices
Definition: segments.h:122
Definition: segments.h:59
Definition: segments.h:46
std::unordered_map< std::string, Class > classes
Definition: segments.h:70
void shift(uintptr_t offset)
Definition: segments.cpp:276
Definition: segments.h:97
Class cast_class
Definition: segments.h:52
Definition: object.h:76
r_code::vector< std::string > class_names
Definition: segments.h:73
size_t size() const
Definition: vector.h:44
size_t get_size()
Definition: segments.cpp:385
Definition: list.h:42
Class _class
Definition: segments.h:51
uint64_t timestamp
Definition: segments.h:135
Definition: object.h:172
r_code::vector< Class > classes_by_opcodes
Definition: segments.h:76
Definition: segments.h:108
std::unordered_map< uintptr_t, std::string > symbols
Definition: segments.h:110
r_code::vector< uintptr_t > objects
Definition: segments.h:88
r_code::vector< std::string > operator_names
Definition: segments.h:74
r_code::vector< SysObject * > objects
Definition: segments.h:99
Definition: image.h:41
ObjectNames object_names
Definition: segments.h:133
void read(uintptr_t *data, uintptr_t size)
Definition: segments.cpp:288
uintptr_t index
Definition: segments.h:50
CodeSegment code_segment
Definition: segments.h:132
void load(I *image)
Definition: segments.h:169
void read(uintptr_t *data)
Definition: segments.cpp:370
size_t get_size()
Definition: segments.cpp:329