Debian Codegraph @

src/bin/dwarfgen/createirepformfrombinary.cc

raw
1 /*
2 Copyright (C) 2010-2018 David Anderson. All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following
6 conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following
11 disclaimer in the
12 documentation and/or other materials provided
13 with the distribution.
14 * Neither the name of the example nor the
15 names of its contributors may be used to endorse
16 or promote products
17 derived from this software without specific
18 prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY David Anderson ''AS IS''
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
22 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL David Anderson BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
30 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 OF SUCH DAMAGE.
34
35 */
36 // createirepformfrombinary.cc
37 // Reads an object and inserts its dwarf data into
38 // an object intended to hold all the dwarf data.
39
40 #include "config.h"
41
42 /* Windows specific header files */
43 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
44 #include "stdafx.h"
45 #endif /* HAVE_STDAFX_H */
46
47 #ifdef HAVE_UNITSTD_H
48 #include <unistd.h>
49 #endif
50 #include <stdlib.h> /* for exit() */
51 #include <iostream>
52 #include <string>
53 #include <list>
54 #include <map>
55 #include <vector>
56 #include <string.h> // For memset etc
57 #include "strtabdata.h"
58 #include "dwarf.h"
59 #include "libdwarf.h"
60 #include "libdwarfp.h"
61 #include "libdwarf_private.h"
62 #include "irepresentation.h"
63 #include "createirepfrombinary.h"
64
65 // Microsoft preprocessor definition that wreaks havoc
66 #ifdef interface
67 #undef interface
68 #endif
69
70 using std::string;
71 using std::cout;
72 using std::cerr;
73 using std::endl;
74 using std::vector;
75 using std::list;
76
77 // THis should instantiated only locally, not with 'new'.
78 // It should not be copied.
79 class IRFormInterface {
80 public:
81 IRFormInterface(Dwarf_Debug dbg,
82 Dwarf_Attribute attr,IRCUdata &cudata,IRAttr & irattr):
83 dbg_(dbg),attr_(attr),cudata_(cudata),irattr_(irattr) {};
84 // Do not free anything we did not create here.
85 ~IRFormInterface() {};
86 Dwarf_Debug dbg_;
87 Dwarf_Attribute attr_;
88 IRCUdata &cudata_;
89 IRAttr &irattr_;
90 private:
91
92 // Do not instantiate.
93 IRFormInterface();
94 IRFormInterface& operator= (const IRFormInterface &ir);
95
96 };
97
98 IRForm *formFactory(Dwarf_Debug dbg,
99 Dwarf_Attribute attr,IRCUdata &cudata,IRAttr & irattr)
100 {
101 IRFormInterface interface(dbg,attr,cudata,irattr);
102 enum Dwarf_Form_Class cl = irattr.getFormClass();
103
104 switch(cl) {
105 case DW_FORM_CLASS_UNKNOWN:
106 break;
107 case DW_FORM_CLASS_ADDRESS:
108 {
109 IRFormAddress *f = new IRFormAddress(&interface);
110 // FIXME: do we need to do anything about symbol here?
111 // Yes, if it has a relocation.
112 return f;
113 }
114 break;
115 case DW_FORM_CLASS_EXPRLOC:
116 case DW_FORM_CLASS_BLOCK:
117 {
118 IRFormBlock *f = new IRFormBlock(&interface);
119 return f;
120 }
121 break;
122 case DW_FORM_CLASS_CONSTANT:
123 {
124 IRFormConstant *f = new IRFormConstant(&interface);
125 return f;
126 }
127 break;
128 case DW_FORM_CLASS_FLAG:
129 {
130 IRFormFlag *f = new IRFormFlag(&interface);
131 return f;
132 }
133 break;
134 case DW_FORM_CLASS_LINEPTR:
135 {
136 IRFormLinePtr *f = new IRFormLinePtr(&interface);
137 return f;
138 }
139 break;
140 case DW_FORM_CLASS_LOCLISTPTR:
141 {
142 IRFormLoclistPtr *f = new IRFormLoclistPtr(&interface);
143 return f;
144 }
145 break;
146 case DW_FORM_CLASS_MACPTR:
147 {
148 IRFormMacPtr *f = new IRFormMacPtr(&interface);
149 return f;
150 }
151 break;
152 case DW_FORM_CLASS_RANGELISTPTR:
153 {
154 IRFormRangelistPtr *f = new IRFormRangelistPtr(
155 &interface);
156 return f;
157 }
158 break;
159 case DW_FORM_CLASS_REFERENCE:
160 {
161 IRFormReference *f = new IRFormReference(&interface);
162 return f;
163 }
164 break;
165 case DW_FORM_CLASS_STRING:
166 {
167 IRFormString *f = new IRFormString(&interface);
168 return f;
169 }
170 break;
171 case DW_FORM_CLASS_FRAMEPTR: /* SGI/IRIX extension. */
172 {
173 IRFormFramePtr *f = new IRFormFramePtr(&interface);
174 return f;
175 }
176 break;
177 default:
178 break;
179 }
180 return new IRFormUnknown();
181 }
182
183 static void
184 extractInterafaceForms(IRFormInterface *interface,
185 Dwarf_Half *finalform,
186 Dwarf_Half *initialform)
187 {
188 Dwarf_Error error = 0;
189 int res = dwarf_whatform(interface->attr_,finalform,&error);
190 if (res != DW_DLV_OK) {
191 cerr << "Unable to get attr form " << endl;
192 exit(1);
193 }
194
195 res = dwarf_whatform_direct(interface->attr_,initialform,&error);
196 if (res != DW_DLV_OK) {
197 cerr << "Unable to get attr direct form " << endl;
198 exit(1);
199 }
200 }
201
202 IRFormAddress::IRFormAddress(IRFormInterface * interface)
203 {
204 Dwarf_Addr val = 0;
205 Dwarf_Error error = 0;
206 Dwarf_Half finalform = 0;
207 Dwarf_Half initialform = 0;
208 extractInterafaceForms(interface,&finalform,&initialform);
209 setFinalForm(finalform);
210 setInitialForm(initialform);
211 formclass_ = DW_FORM_CLASS_ADDRESS;
212 address_ = 0;
213
214 int res = dwarf_formaddr(interface->attr_,&val, &error);
215 if (res != DW_DLV_OK) {
216 cerr << "Unable to read flag value. Impossible error.\n"
217 << " line " << __LINE__ << " file " <<
218 __FILE__ << endl;
219 exit(1);
220 }
221 // FIXME do we need to do anything about the symbol here?
222 // It might have a relocation and we should determine that.
223 address_ = val;
224 }
225 IRFormBlock::IRFormBlock(IRFormInterface * interface)
226 {
227 Dwarf_Half finalform = 0;
228 Dwarf_Half initialform = 0;
229 extractInterafaceForms(interface,&finalform,&initialform);
230 setFinalForm(finalform);
231 setInitialForm(initialform);
232 formclass_=DW_FORM_CLASS_BLOCK;
233 fromloclist_= 0;
234 sectionoffset_=0;
235
236 Dwarf_Block *blockptr = 0;
237 Dwarf_Error error = 0;
238 int res = dwarf_formblock(interface->attr_,&blockptr, &error);
239 if (res != DW_DLV_OK) {
240 cerr << "Unable to read block . Impossible error.\n"
241 << " line " << __LINE__ << " file " <<
242 __FILE__ << endl;
243 exit(1);
244 }
245 insertBlock(blockptr);
246 dwarf_dealloc(interface->dbg_,blockptr,DW_DLA_BLOCK);
247 }
248 IRFormConstant::IRFormConstant(IRFormInterface * interface)
249 {
250 Dwarf_Half finalform = 0;
251 Dwarf_Half initialform = 0;
252 extractInterafaceForms(interface,&finalform,&initialform);
253 setFinalForm(finalform);
254 setInitialForm(initialform);
255 formclass_=DW_FORM_CLASS_CONSTANT;
256 signedness_=SIGN_NOT_SET;
257 uval_=0;
258 sval_=0;
259 Dwarf_Error error = 0;
260
261 if (finalform == DW_FORM_data16) {
262 Dwarf_Form_Data16 data16;
263 int rd16 = dwarf_formdata16(interface->attr_,&data16,
264 &error);
265 if (rd16 != DW_DLV_OK) {
266 cerr << "Unable to read constant data16 value. "
267 "Impossible error.\n" << endl;
268 exit(1);
269 }
270 setValues16(&data16, SIGN_UNKNOWN);
271 return;
272 }
273 enum Signedness oursign = SIGN_NOT_SET;
274 Dwarf_Unsigned uval = 0;
275 Dwarf_Signed sval = 0;
276 int ress = dwarf_formsdata(interface->attr_, &sval,&error);
277 if (ress == DW_DLV_ERROR) {
278 Dwarf_Debug dbg = interface->dbg_;
279 dwarf_dealloc_error(dbg,error);
280 error = 0;
281 ress = DW_DLV_NO_ENTRY;
282 }
283 int resu = dwarf_formudata(interface->attr_, &uval,&error);
284 if (resu == DW_DLV_ERROR) {
285 Dwarf_Debug dbg = interface->dbg_;
286 dwarf_dealloc_error(dbg,error);
287 error = 0;
288 resu = DW_DLV_NO_ENTRY;
289 }
290 if (resu == DW_DLV_OK ) {
291 if (ress == DW_DLV_OK) {
292 if (finalform == DW_FORM_sdata) {
293 /* sval set */
294 oursign = SIGNED;
295 } else {
296 oursign = SIGN_UNKNOWN;
297 }
298 } else {
299 oursign = UNSIGNED;
300 sval = static_cast<Dwarf_Signed>(uval);
301 }
302 } else {
303 if (ress == DW_DLV_OK) {
304 oursign = SIGNED;
305 uval = static_cast<Dwarf_Unsigned>(sval);
306 } else {
307 cerr << "Unable to read constant value. "
308 "Impossible error."
309 << endl;
310 exit(1);
311 }
312 }
313 setValues(sval, uval, oursign);
314 }
315 IRFormExprloc::IRFormExprloc(IRFormInterface * interface)
316 {
317 Dwarf_Unsigned len = 0;
318 Dwarf_Ptr data = 0;
319 Dwarf_Error error = 0;
320 Dwarf_Half finalform = 0;
321 Dwarf_Half initialform = 0;
322 extractInterafaceForms(interface,&finalform,&initialform);
323 setFinalForm(finalform);
324 setInitialForm(initialform);
325 formclass_=DW_FORM_CLASS_EXPRLOC;
326
327 int res = dwarf_formexprloc(interface->attr_,&len, &data, &error);
328 if (res != DW_DLV_OK) {
329 cerr << "Unable to read flag value. Impossible error.\n"
330 << " line " << __LINE__ << " file " <<
331 __FILE__ << endl;
332 exit(1);
333 }
334 // FIXME: Would be nice to expand to the expression details
335 // instead of just a block of bytes.
336 insertBlock(len,data);
337 }
338 IRFormFlag::IRFormFlag(IRFormInterface * interface)
339 {
340 Dwarf_Bool flagval = 0;
341 Dwarf_Half finalform = 0;
342 Dwarf_Half initialform = 0;
343 extractInterafaceForms(interface,&finalform,&initialform);
344 setFinalForm(finalform);
345 setInitialForm(initialform);
346 formclass_=DW_FORM_CLASS_FLAG;
347 flagval_=0;
348
349 Dwarf_Error error = 0;
350 int res = dwarf_formflag(interface->attr_,&flagval, &error);
351 if (res != DW_DLV_OK) {
352 cerr << "Unable to read flag value. Impossible error.\n"
353 << " line " << __LINE__ << " file " <<
354 __FILE__ << endl;
355 exit(1);
356 }
357 setFlagVal(flagval);
358 setFinalForm(finalform);
359 setInitialForm(initialform);
360
361 }
362
363 // We are simply assuming (here) that the value is a global offset
364 // to some section or other.
365 // Calling code ensures that is true.
366 //
367 static Dwarf_Unsigned
368 get_section_offset(IRFormInterface * interface)
369 {
370 Dwarf_Unsigned uval = 0;
371 Dwarf_Signed sval = 0;
372 Dwarf_Error error = 0;
373 Dwarf_Off sectionoffset = 0;
374 int resu = 0;
375 int ress = 0;
376
377 // The following allows more sorts of value than
378 // we really want to allow here, but that is
379 // harmless, we believe.
380 int resgf = dwarf_global_formref(interface->attr_,
381 &sectionoffset, &error);
382 if (resgf == DW_DLV_OK ) {
383 return sectionoffset;
384 }
385 resu = dwarf_formudata(interface->attr_, &uval,&error);
386 if (resu != DW_DLV_OK ) {
387 if (resu == DW_DLV_ERROR){
388 Dwarf_Debug dbg = interface->dbg_;
389 dwarf_dealloc_error(dbg,error);
390 error = 0;
391 resu = DW_DLV_NO_ENTRY;
392 }
393 ress = dwarf_formsdata(interface->attr_, &sval,&error);
394 if (ress == DW_DLV_OK) {
395 uval = static_cast<Dwarf_Unsigned>(sval);
396 } else {
397 if (ress == DW_DLV_ERROR) {
398 Dwarf_Debug dbg = interface->dbg_;
399 dwarf_dealloc_error(dbg,error);
400 error = 0;
401 ress = DW_DLV_NO_ENTRY;
402 }
403 cerr << "Unable to read constant offset value. "
404 "Impossible error.\n"
405 << endl;
406 exit(1);
407 }
408 }
409 return uval;
410 }
411 IRFormLoclistPtr::IRFormLoclistPtr(IRFormInterface * interface)
412 {
413 Dwarf_Unsigned uval = get_section_offset(interface);
414 setOffset(uval);
415 Dwarf_Half finalform = 0;
416 Dwarf_Half initialform = 0;
417 extractInterafaceForms(interface,&finalform,&initialform);
418 setFinalForm(finalform);
419 setInitialForm(initialform);
420 formclass_=DW_FORM_CLASS_LOCLISTPTR;
421 loclist_offset_=0;
422 }
423
424 // When emitting line data from the producer, this
425 // attribute is automatically emitted by the transform_to_disk
426 // function if there is line data in the generated CU,
427 // not created by interpreting this input attribute.
428 IRFormLinePtr::IRFormLinePtr(IRFormInterface * interface)
429 {
430 Dwarf_Unsigned uval = get_section_offset(interface);
431 setOffset(uval);
432 Dwarf_Half finalform = 0;
433 Dwarf_Half initialform = 0;
434 extractInterafaceForms(interface,&finalform,&initialform);
435 setFinalForm(finalform);
436 setInitialForm(initialform);
437 formclass_=DW_FORM_CLASS_LINEPTR;
438
439 }
440 IRFormMacPtr::IRFormMacPtr(IRFormInterface * interface)
441 {
442 Dwarf_Unsigned uval = get_section_offset(interface);
443 setOffset(uval);
444 Dwarf_Half finalform = 0;
445 Dwarf_Half initialform = 0;
446 extractInterafaceForms(interface,&finalform,&initialform);
447 setFinalForm(finalform);
448 setInitialForm(initialform);
449 formclass_=DW_FORM_CLASS_MACPTR;
450 macro_offset_=0;
451
452 }
453 IRFormRangelistPtr::IRFormRangelistPtr(IRFormInterface * interface)
454 {
455 Dwarf_Unsigned uval = get_section_offset(interface);
456 setOffset(uval);
457 Dwarf_Half finalform = 0;
458 Dwarf_Half initialform = 0;
459 extractInterafaceForms(interface,&finalform,&initialform);
460 setFinalForm(finalform);
461 setInitialForm(initialform);
462 formclass_=DW_FORM_CLASS_RANGELISTPTR;
463 rangelist_offset_=0;
464
465 }
466 IRFormFramePtr::IRFormFramePtr(IRFormInterface * interface)
467 {
468 Dwarf_Unsigned uval = get_section_offset(interface);
469 setOffset(uval);
470 Dwarf_Half finalform = 0;
471 Dwarf_Half initialform = 0;
472 extractInterafaceForms(interface,&finalform,&initialform);
473 setFinalForm(finalform);
474 setInitialForm(initialform);
475 formclass_=DW_FORM_CLASS_FRAMEPTR;
476 frame_offset_=0;
477
478 }
479
480 // This is a .debug_info to .debug_info (or .debug_types
481 // to .debug_types) reference.
482
483 // Since local/global reference target values are not known
484 // till the DIEs are actually emitted,
485 // we eventually use libdwarf (dwarf_get_die_markers) to fix up
486 // the new target value. Here we just record the input value.
487 //
488 IRFormReference::IRFormReference(IRFormInterface * interface)
489 {
490 Dwarf_Off val = 0;
491 Dwarf_Error error = 0;
492 Dwarf_Half finalform = 0;
493 Dwarf_Half initialform = 0;
494 formclass_ = DW_FORM_CLASS_REFERENCE;
495 reftype_ = RT_NONE;
496 globalOffset_ = 0;
497 cuRelativeOffset_ = 0;
498 targetInputDie_= 0;
499 target_die_= 0;
500 initSig8();
501 Dwarf_Bool is_info = TRUE;
502
503 extractInterafaceForms(interface,&finalform,&initialform);
504 setFinalForm(finalform);
505 setInitialForm(initialform);
506 IRCUdata &cudata = interface->cudata_;
507
508 if (finalform == DW_FORM_ref_sig8) {
509 Dwarf_Sig8 val8;
510 int res = dwarf_formsig8(interface->attr_,&val8, &error);
511 if (res != DW_DLV_OK) {
512 cerr << "Unable to read sig8 reference. "
513 "Impossible error.\n"
514 << endl;
515 exit(1);
516 }
517 setSignature(&val8);
518 return;
519 }
520 if (finalform == DW_FORM_ref_addr ||
521 finalform == DW_FORM_data4 ||
522 finalform == DW_FORM_data8) {
523 // FIXME there might be a relocation record.
524 int res = dwarf_global_formref(interface->attr_,&val,
525 &error);
526 if (res != DW_DLV_OK) {
527 cerr << "Unable to read reference. "
528 "Impossible error.\n" << endl;
529 exit(1);
530 }
531 setOffset(val);
532 return;
533 }
534 // Otherwise it is (if a correct FORM for a
535 // .debug_info reference)
536 // a local CU offset, and we record it as such..
537 int res = dwarf_formref(interface->attr_,&val,&is_info,&error);
538 if (res != DW_DLV_OK) {
539 cerr << "Unable to read reference.. "
540 "Impossible error. finalform " <<
541 finalform << endl;
542 exit(1);
543 }
544 setCUOffset(val);
545 cudata.insertLocalReferenceAttrTargetRef(val,this);
546 IRDie *targdie = cudata.getLocalDie(val);
547 if (targdie) {
548 // Record local offset's IRDie when it is already known.
549 setTargetInDie(targdie);
550 }
551 }
552
553 // Global static data used to initialized a sig8 reliably.
554 static Dwarf_Sig8 zero_sig8;
555 // Initialization helper function.
556 void
557 IRFormReference::initSig8()
558 {
559 typeSig8_ = zero_sig8;
560 }
561
562 IRFormString::IRFormString(IRFormInterface * interface)
563 {
564 char *str = 0;
565 Dwarf_Error error = 0;
566 Dwarf_Half finalform = 0;
567 Dwarf_Half initialform = 0;
568 extractInterafaceForms(interface,&finalform,&initialform);
569 setFinalForm(finalform);
570 setInitialForm(initialform);
571 formclass_=DW_FORM_CLASS_STRING;
572 strpoffset_=0;
573
574 int res = dwarf_formstring(interface->attr_,&str, &error);
575 if (res != DW_DLV_OK) {
576 cerr << "Unable to read string. Impossible error.\n" << endl;
577 exit(1);
578 }
579 setString(str);
580 }