Debian Codegraph @

src/bin/dwarfgen/irepdie.h

raw
1 /*
2 Copyright (C) 2010-2018 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 THIS SOFTWARE IS PROVIDED BY David Anderson ''AS IS''
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
21 NO EVENT SHALL David Anderson BE LIABLE FOR ANY DIRECT,
22 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
29 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 */
32
33 //
34 // irepdie.h
35 //
36 //
37 class IRCUdata;
38 class IRDie;
39
40 class IRAttr {
41 public:
42 IRAttr():attr_(0),finalform_(0),initialform_(0),
43 formclass_(DW_FORM_CLASS_UNKNOWN),formdata_(0) {
44 };
45 IRAttr(Dwarf_Half attr,Dwarf_Half finalform,
46 Dwarf_Half initialform):
47 attr_(attr),finalform_(finalform),initialform_(initialform),
48 formclass_(DW_FORM_CLASS_UNKNOWN),formdata_(0) {
49 };
50 IRAttr(const IRAttr &r) { // copy constructor
51 attr_ = r.attr_;
52 finalform_ = r.finalform_;
53 initialform_ = r.initialform_;
54 formclass_ = r.formclass_;
55 if (r.formdata_) {
56 formdata_ = r.formdata_->clone();
57 } else {
58 formdata_ = 0;
59 }
60 };
61 ~IRAttr() {
62 delete formdata_; };
63 IRAttr & operator=( const IRAttr &r) {
64 if (this == &r) {
65 return *this;
66 }
67 attr_ = r.attr_;
68 finalform_ = r.finalform_;
69 initialform_ = r.initialform_;
70 formclass_ = r.formclass_;
71 if (formdata_) {
72 delete formdata_;
73 formdata_ = r.formdata_->clone();
74 } else {
75 formdata_ = r.formdata_->clone();
76 }
77 return *this;
78 }
79 void setBaseData(Dwarf_Half attr, Dwarf_Half finalform,
80 Dwarf_Half initialform){
81 attr_ = attr;
82 finalform_ = finalform;
83 initialform_ = initialform;
84 };
85 void setFormClass(enum Dwarf_Form_Class cl) {
86 formclass_ = cl;
87 };
88 enum Dwarf_Form_Class getFormClass() const {return formclass_; };
89 void dropFormData() {
90 if (formdata_)
91 {delete formdata_; formdata_ = 0; };
92 }
93 void setFormData(IRForm *f) {
94 if (formdata_) {
95 delete formdata_;
96 formdata_ = 0;
97 }
98 formdata_ = f;
99 };
100 Dwarf_Half getFinalForm() const { return initialform_; };
101 Dwarf_Half getDirectForm() const { return finalform_; };
102 Dwarf_Half getAttrNum() const { return attr_; };
103 IRForm * getFormData() { return formdata_;};
104 private:
105 Dwarf_Half attr_;
106 Dwarf_Half finalform_;
107 // In most cases finalform == initialform form.
108 // Otherwise, initialform == DW_FORM_indirect.
109 Dwarf_Half initialform_;
110 enum Dwarf_Form_Class formclass_;
111 IRForm *formdata_;
112 };
113
114 class IRDie {
115 public:
116 IRDie():tag_(0),globalOffset_(0), cuRelativeOffset_(0),
117 generatedDie_(0) {};
118 ~IRDie() {};
119 void addChild(const IRDie & newdie ) {
120 children_.push_back(newdie);
121 };
122 std::string getName() {
123 std::list<IRAttr>::iterator it = attrs_.begin();
124 for ( ; it != attrs_.end() ; ++it) {
125 if (it->getAttrNum() == DW_AT_name) {
126 IRForm *f = it->getFormData();
127 const IRFormString * isv =
128 dynamic_cast<const IRFormString *>(f);
129 if (isv) {
130 return isv->getString();
131 }
132 }
133 }
134 return "";
135 };
136 std::list<IRAttr> & getAttributes() {return attrs_; };
137 std::list<IRDie> & getChildren() {return children_; };
138 bool hasNewestChild(IRDie **lastch) { size_t N = children_.size();
139 if (N < 1) {
140 return false;
141 }
142 *lastch = &children_.back();
143 return true;
144 };
145 // lastChild and lastAttr will throw if no entry exists.
146 IRDie &lastChild() { return children_.back(); };
147 IRAttr &lastAttr() { return attrs_.back(); };
148 void setBaseData(Dwarf_Half tag,Dwarf_Unsigned goff,
149 Dwarf_Unsigned cuoff) {
150 tag_ = tag;
151 globalOffset_=goff;
152 cuRelativeOffset_ = cuoff;
153 };
154 Dwarf_Unsigned getGlobalOffset() const { return globalOffset_;};
155 Dwarf_Unsigned getCURelativeOffset() const {
156 return cuRelativeOffset_;};
157 void setGeneratedDie(Dwarf_P_Die p_die) {
158 generatedDie_ = p_die;};
159 Dwarf_P_Die getGeneratedDie() const { return generatedDie_;};
160 unsigned getTag() {return tag_; }
161
162 private:
163 // We rely on the IRDie container being one which does not
164 // invalidate pointers with addition/deletion.
165 std::list<IRDie> children_;
166
167 std::list<IRAttr> attrs_;
168 unsigned tag_;
169 // The following are data from input.
170 Dwarf_Unsigned globalOffset_;
171 Dwarf_Unsigned cuRelativeOffset_;
172
173 // the following is generated during output.
174 Dwarf_P_Die generatedDie_;
175 };
176
177 struct OffsetFormEntry {
178 OffsetFormEntry(): off_(0),form_(0){};
179 OffsetFormEntry(Dwarf_Unsigned o,
180 IRFormReference* f): off_(o),form_(f){};
181 ~OffsetFormEntry(){};
182
183 Dwarf_Unsigned off_;
184 IRFormReference *form_;
185 };
186 struct ClassReferenceFixupData {
187 ClassReferenceFixupData():
188 dbg_(0),
189 attrnum_(0),
190 sourcedie_(0),
191 target_(0) {}
192 ~ClassReferenceFixupData(){};
193 ClassReferenceFixupData(
194 Dwarf_P_Debug dbg,
195 Dwarf_Half attrnum,
196 Dwarf_P_Die sourcedie,
197 IRDie *d):
198 dbg_(dbg),
199 attrnum_(attrnum),
200 sourcedie_(sourcedie),
201 target_(d) {};
202 Dwarf_P_Debug dbg_;
203 // The source die and attrnum suffice because the definition of
204 // DWARF guarantees only one attribute of any
205 // given attribute number
206 // can exist on a given DIE.
207 Dwarf_Half attrnum_;
208 Dwarf_P_Die sourcedie_;
209 IRDie *target_;
210 };
211
212 class IRCUdata {
213 public:
214 IRCUdata():
215 cu_header_length_(0),
216 abbrev_offset_(0),
217 next_cu_header_offset_(0),
218 version_stamp_(0),
219 address_size_(0),
220 length_size_(0),
221 extension_size_(0),
222 has_macrodata_(false),
223 macrodata_offset_(0),
224 has_linedata_(false),
225 linedata_offset_(0),
226 cudie_offset_(0),
227 dwarf32bit_(0)
228 {};
229
230 IRCUdata(Dwarf_Unsigned len,Dwarf_Half version,
231 Dwarf_Unsigned abbrev_offset,
232 Dwarf_Half addr_size,
233 Dwarf_Half length_size,
234 Dwarf_Half extension_size,
235 Dwarf_Unsigned next_cu_header):
236
237 cu_header_length_(len),
238 abbrev_offset_(abbrev_offset),
239 next_cu_header_offset_(next_cu_header),
240 version_stamp_(version),
241 address_size_(addr_size),
242 length_size_(length_size),
243 extension_size_(extension_size),
244 has_macrodata_(false),
245 macrodata_offset_(0),
246 has_linedata_(false),
247 linedata_offset_(0),
248 cudie_offset_(0) ,
249 dwarf32bit_(0){};
250 ~IRCUdata() { };
251 bool hasMacroData(Dwarf_Unsigned *offset_out,
252 Dwarf_Unsigned *cudie_off) {
253 *offset_out = macrodata_offset_;
254 *cudie_off = cudie_offset_;
255 return has_macrodata_;
256 }
257 bool hasLineData(Dwarf_Unsigned *offset_out,
258 Dwarf_Unsigned *cudie_off) {
259 *offset_out = linedata_offset_;
260 *cudie_off = cudie_offset_;
261 return has_linedata_;
262 }
263 void setMacroData(Dwarf_Unsigned offset,Dwarf_Unsigned cudieoff) {
264 has_macrodata_ = true;
265 macrodata_offset_ = offset;
266 cudie_offset_ = cudieoff;
267 };
268 void setLineData(Dwarf_Unsigned offset,Dwarf_Unsigned cudieoff) {
269 has_linedata_ = true;
270 linedata_offset_ = offset;
271 cudie_offset_ = cudieoff;
272 };
273 IRDie & baseDie() { return cudie_; };
274 Dwarf_Half getVersionStamp() { return version_stamp_; };
275 Dwarf_Half getOffsetSize() { return length_size_; };
276 Dwarf_Unsigned getCUdieOffset() { return cudie_offset_; };
277 IRCULineData & getCULines() { return cu_lines_; };
278
279 void insertLocalDieOffset(Dwarf_Unsigned localoff,IRDie* dieptr) {
280 cuOffInLocalToIRDie_[localoff] = dieptr;
281 };
282 void insertLocalReferenceAttrTargetRef(Dwarf_Unsigned localoff,
283 IRFormReference* attrptr) {
284
285 cuOffInLocalToIRFormRef_.push_back(OffsetFormEntry(localoff,
286 attrptr));
287 };
288 IRDie * getLocalDie(Dwarf_Unsigned localoff) {
289 std::map<Dwarf_Unsigned,IRDie*>::iterator pos;
290 pos = cuOffInLocalToIRDie_.find(localoff);
291 if (pos != cuOffInLocalToIRDie_.end()) {
292 return pos->second;
293 }
294 return NULL;
295 };
296 void insertClassReferenceFixupData(ClassReferenceFixupData &c) {
297 classReferenceFixupList_.push_back(c);
298 }
299 void updateClassReferenceTargets();
300 std::string getCUName() {
301 return cudie_.getName();
302 };
303 // Use cuOffInLocalToIRDie_ and
304 // cuOffInLocalToIRAttr_ to update attr targets.
305 void updateReferenceAttrDieTargets() {
306 for (std::list<OffsetFormEntry>::iterator it =
307 cuOffInLocalToIRFormRef_.begin();
308 it != cuOffInLocalToIRFormRef_.end();
309 ++it) {
310 IRFormReference* r = it->form_;
311 IRDie * tdie = getLocalDie(it->off_);
312 if (tdie) {
313 r->setTargetInDie(tdie);
314 } else {
315 // Missing die in r
316 // Should be impossible.
317 }
318 }
319 }
320 Dwarf_Unsigned get_cu_header_length() {
321 return cu_header_length_;};
322 Dwarf_Unsigned get_abbrev_offset() {
323 return abbrev_offset_;};
324 Dwarf_Unsigned get_next_cu_header_offset() {
325 return next_cu_header_offset_;};
326 Dwarf_Unsigned get_version_stamp() {
327 return version_stamp_;};
328 Dwarf_Unsigned get_address_size() {
329 return address_size_;};
330 Dwarf_Unsigned get_length_size() {
331 return length_size_;};
332 Dwarf_Unsigned get_extension_size() {
333 return extension_size_;};
334 bool get_has_macrodata() {
335 return has_macrodata_;};
336 Dwarf_Unsigned get_macrodata_offset() {
337 return macrodata_offset_;};
338 bool get_has_linedata() {
339 return has_linedata_;};
340 Dwarf_Unsigned get_linedata_offset() {
341 return linedata_offset_;};
342 Dwarf_Unsigned get_cudie_offset() {
343 return cudie_offset_;};
344 bool get_dwarf32bit() {
345 return dwarf32bit_;};
346
347 private:
348 Dwarf_Unsigned cu_header_length_;
349 Dwarf_Unsigned abbrev_offset_;
350 Dwarf_Unsigned next_cu_header_offset_;
351 Dwarf_Half version_stamp_;
352 Dwarf_Half address_size_;
353 Dwarf_Half length_size_;
354 Dwarf_Half extension_size_;
355 bool has_macrodata_;
356 Dwarf_Unsigned macrodata_offset_;
357 bool has_linedata_;
358 Dwarf_Unsigned linedata_offset_;
359 Dwarf_Unsigned cudie_offset_;
360 bool dwarf32bit_;
361 IRCULineData cu_lines_;
362 // If true, is 32bit dwarf,else 64bit.
363 // Gives the size of a reference.
364 IRDie cudie_;
365
366 // Refers to cu-local offsets in the input CU and which DIE
367 // in the input they target for this CU.
368 // Used to find the target DIE for the IRAttrs
369 // referenced by cuOffInLocalToIRAttr_
370 std::map<Dwarf_Unsigned,IRDie*> cuOffInLocalToIRDie_;
371
372 // Refers to IRAttrs which make a CU local reference
373 // meaning CLASS_REFERENCE IRFormReference to a cu-local die
374 // Once Input dies read in this and cuOffInLocalToIRDie_
375 // are used to update the IRAttr itself.
376 std::list<OffsetFormEntry> cuOffInLocalToIRFormRef_;
377
378 // The data needed to get the Dwarf_P_Die set for
379 // some class reference instances.
380 std::list<ClassReferenceFixupData> classReferenceFixupList_;
381 };
382
383 class IRDInfo {
384 public:
385 IRDInfo() {};
386 ~IRDInfo() {};
387 IRCUdata &lastCU() { return cudata_.back(); }
388 std::list<IRCUdata>& getCUData() {return cudata_; };
389 private:
390 std::list<IRCUdata> cudata_;
391 };