Debian Codegraph @

src/bin/dwarfgen/irepframe.h

raw
1 /*
2 Copyright (C) 2010-2013 David Anderson. All rights reserved.
3
4 Redistribution and use in source and binary forms, with
5 or without modification, are permitted provided that the
6 following conditions are met:
7
8 * Redistributions of source code must retain the above
9 copyright notice, this list of conditions and the following
10 disclaimer.
11
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials
15 provided with the distribution.
16
17 * Neither the name of the example nor the names of its
18 contributors may be used to endorse or promote products
19 derived from this software without specific prior written
20 permission.
21
22 THIS SOFTWARE IS PROVIDED BY David Anderson ''AS IS''
23 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
24 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
26 NO EVENT SHALL David Anderson BE LIABLE FOR ANY DIRECT,
27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
29 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 */
37
38 //
39 // irepframe.h
40 //
41
42 class IRCie {
43 public:
44 IRCie(): cie_byte_length_(0), version_(0),
45 code_alignment_factor_(1),
46 data_alignment_factor_(1),
47 return_address_register_rule_(0)
48 {};
49 IRCie(Dwarf_Unsigned length, Dwarf_Unsigned version,
50 const std::string &augmentation, Dwarf_Unsigned code_align,
51 Dwarf_Signed data_align, Dwarf_Half return_reg_rule,
52 const void * init_instrs, Dwarf_Unsigned instrs_len):
53 cie_byte_length_(length), version_(version),
54 augmentation_(augmentation),
55 code_alignment_factor_(code_align),
56 data_alignment_factor_(data_align),
57 return_address_register_rule_(return_reg_rule)
58 {
59 const Dwarf_Small *x =
60 reinterpret_cast<const Dwarf_Small *>(init_instrs);
61 for (Dwarf_Unsigned i = 0; i < instrs_len; ++i) {
62 initial_instructions_.push_back(x[i]);
63 }
64 }
65 void insert_fde_index(unsigned i) { fde_index_.push_back(i); };
66 ~IRCie() {};
67 void get_basic_cie_data(Dwarf_Unsigned * version,
68 std::string * aug,
69 Dwarf_Unsigned * code_align,
70 Dwarf_Signed * data_align,
71 Dwarf_Half * ret_addr_reg) {
72 *version = version_;
73 *aug = augmentation_;
74 *code_align = code_alignment_factor_;
75 *data_align = data_alignment_factor_;
76 *ret_addr_reg = return_address_register_rule_;
77 }
78 void get_init_instructions(Dwarf_Unsigned *len,
79 void **bytes) {
80 *len = initial_instructions_.size();
81 *bytes = reinterpret_cast<void *>(&initial_instructions_[0]);
82 };
83 Dwarf_Unsigned get_cie_byte_length() {return cie_byte_length_;}
84 Dwarf_Unsigned get_version() {return version_;}
85 std::string& get_augmentation() {return augmentation_;}
86 Dwarf_Unsigned get_code_aligmnent_factor() {
87 return code_alignment_factor_;}
88 Dwarf_Unsigned get_data_aligmnent_factor() {
89 return data_alignment_factor_;}
90 Dwarf_Half get_return_address_register_rule() {
91 return return_address_register_rule_;}
92 std::vector<Dwarf_Small>& get_initial_instructions() {
93 return initial_instructions_;}
94 std::vector<unsigned int>& get_fde_index() {
95 return fde_index_;}
96
97 private:
98 // Byte length 0 if not known yet.
99 Dwarf_Unsigned cie_byte_length_;
100 Dwarf_Unsigned version_;
101 std::string augmentation_;
102 Dwarf_Unsigned code_alignment_factor_;
103 Dwarf_Signed data_alignment_factor_;
104 Dwarf_Half return_address_register_rule_;
105 std::vector<Dwarf_Small> initial_instructions_;
106 // fde_index is the array of indexes into fdedata_
107 // that are fdes used by this cie.
108 std::vector<unsigned int> fde_index_;
109 };
110 class IRFde {
111 public:
112 IRFde(): low_pc_(0), func_length_(0),
113 cie_offset_(0), cie_index_(-1),
114 fde_offset_(0) {};
115 IRFde(Dwarf_Addr low_pc,Dwarf_Unsigned func_length,
116 Dwarf_Ptr fde_bytes, Dwarf_Unsigned fde_length,
117 Dwarf_Off cie_offset,Dwarf_Signed cie_index_in,
118 Dwarf_Off fde_offset):
119 low_pc_(low_pc),
120 func_length_(func_length),
121 cie_offset_(cie_offset), cie_index_(cie_index_in),
122 fde_offset_(fde_offset) {
123 const Dwarf_Small *x =
124 reinterpret_cast<const Dwarf_Small *>(fde_bytes);
125 for (Dwarf_Unsigned i = 0; i < fde_length; ++i) {
126 fde_bytes_.push_back(x[i]);
127 }
128 };
129 ~IRFde() {};
130 Dwarf_Unsigned cie_index() { return cie_index_; };
131 void get_fde_base_data(Dwarf_Addr *lowpc,
132 Dwarf_Unsigned * funclen,
133 Dwarf_Unsigned *cie_index_input) {
134 *lowpc = low_pc_;
135 *funclen = func_length_;
136 *cie_index_input = cie_index_;
137 };
138 void get_fde_instrs_into_ir(Dwarf_Ptr ip,Dwarf_Unsigned len ) {
139 const Dwarf_Small *x =
140 reinterpret_cast<const Dwarf_Small *>(ip);
141 for (Dwarf_Unsigned i = 0; i < len; ++i) {
142 fde_instrs_.push_back(x[i]);
143 }
144 };
145
146 void get_fde_instructions(Dwarf_Unsigned *len,
147 void **bytes) {
148 *len = fde_instrs_.size();
149 *bytes = reinterpret_cast<void *>(&fde_instrs_[0]);
150 };
151 void fde_instrs () {
152 };
153 Dwarf_Off get_fde_offset() {
154 return fde_offset_;}
155 Dwarf_Off get_cie_offset() {
156 return cie_offset_;}
157
158 private:
159 Dwarf_Addr low_pc_;
160 Dwarf_Unsigned func_length_;
161 // fde_bytes_ may be empty if content bytes not yet created.
162 std::vector<Dwarf_Small> fde_bytes_;
163
164 // fde_instrs_ is simply a vector of bytes.
165 // it might be good to actually parse the
166 // instructions.
167 std::vector<Dwarf_Small> fde_instrs_;
168 // cie_offset may be 0 if not known yet.
169 Dwarf_Off cie_offset_;
170 // cie_index is the index in ciedata_ of
171 // the applicable CIE. Begins with index 0.
172 Dwarf_Signed cie_index_;
173 // fde_offset may be 0 if not yet known.
174 Dwarf_Off fde_offset_;
175 };
176
177 class IRFrame {
178 public:
179 IRFrame() {};
180 ~IRFrame() {};
181 void insert_cie(IRCie &cie) {
182 ciedata_.push_back(cie);
183 }
184 void insert_fde(IRFde &fdedata) {
185 fdedata_.push_back(fdedata);
186 unsigned findex = fdedata_.size() -1;
187 Dwarf_Signed cindex = fdedata.cie_index();
188 if (cindex != -1) {
189 IRCie & mycie = ciedata_[cindex];
190 mycie.insert_fde_index(findex);
191 }
192 }
193 std::vector<IRCie> &get_cie_vec() { return ciedata_; };
194 std::vector<IRFde> &get_fde_vec() { return fdedata_; };
195 private:
196 std::vector<IRCie> ciedata_;
197 std::vector<IRFde> fdedata_;
198 };