Debian Codegraph @

src/bin/dwarfgen/ireptodbg.cc

raw
1 /*
2 Copyright (C) 2010-2019 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'' AND ANY
23 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David
26 Anderson BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35 */
36
37 // ireptodbg.cc
38
39 #include "config.h"
40
41 /* Windows specific header files */
42 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
43 #include "stdafx.h"
44 #endif /* HAVE_STDAFX_H */
45 #if HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 #include <stdlib.h> /* for exit() */
49 #include <iostream>
50 #include <string>
51 #include <list>
52 #include <map>
53 #include <vector>
54 #include <string.h> // For memset etc
55 #include "strtabdata.h"
56 #include "dwarf.h"
57 #include "libdwarf.h"
58 #include "libdwarfp.h"
59 #include "libdwarf_private.h"
60 #include "irepresentation.h"
61 #include "ireptodbg.h"
62 #include "irepattrtodbg.h"
63 #include "general.h"
64
65 using std::string;
66 using std::cout;
67 using std::cerr;
68 using std::endl;
69 using std::vector;
70 using std::map;
71 using std::list;
72 using std::map;
73
74 static Dwarf_Error error;
75
76 typedef std::map<std::string,unsigned> pathToUnsignedType;
77
78 // This set of operations is meaningless.
79 const int skipscount = 10;
80 struct Skips {
81 Dwarf_Small opcode;
82 Dwarf_Signed sval1;
83 Dwarf_Unsigned uval2;
84 } skips[10] = {
85 { DW_OP_breg3,-4,0},
86 { DW_OP_dup,0,0},
87 { DW_OP_const4s,-1000,0},
88 { DW_OP_dup,0,0},
89 { DW_OP_const4s,4000,0},
90 { DW_OP_swap,0,0},
91 { DW_OP_skip,-1,0}, //This will be an error for dwarfdump to note,
92 //not usable offset.
93 { DW_OP_bra,2,0}, //This will be an error for dwarfdump to note,
94 // not usable offset.
95 { DW_OP_const4s,-6000,0},
96 { DW_OP_const4s,8000,0},
97 };
98
99 static int
100 createskipbranchblock(
101 Dwarf_P_Debug dbg,
102 Dwarf_Block &bl)
103 {
104 Dwarf_P_Expr ex = 0;
105 int res = 0;
106 Dwarf_Unsigned stream_len = 0;
107
108 error = 0;
109 res = dwarf_new_expr_a(dbg,&ex, &error);
110 if (res != DW_DLV_OK) {
111 cout << "FAIL dwarf_new_expr_a createskipbranchblock "
112 <<endl;
113 error = 0;
114 return res;
115 }
116 for (int i = 0; i < skipscount; ++i) {
117 struct Skips * sp = skips+i;
118 res = dwarf_add_expr_gen_a(ex,sp->opcode,
119 sp->sval1,sp->uval2,&stream_len,&error);
120 if (res != DW_DLV_OK) {
121 cout <<
122 "FAIL dwarf_add_expr_gen_a createskipbranchblock "
123 << dwarf_errmsg(error) << endl;
124
125 error = 0;
126 return res;
127 }
128 }
129 Dwarf_Unsigned exlen = 0;
130 Dwarf_Small * exptr = 0;
131 res = dwarf_expr_into_block_a(ex, &exlen,&exptr,&error);
132 if (res != DW_DLV_OK) {
133 cout << "FAIL dwarf_expr_into_block_a createskipbranchblock"
134 <<endl;
135 error = 0;
136 return res;
137 }
138 if (exlen != stream_len) {
139 cout << "FAIL createskipbranchblock " <<
140 "block len:"<< exlen <<
141 " streamlen:" <<stream_len <<endl;
142 error = 0;
143 return DW_DLV_ERROR;
144 }
145 error = 0;
146 bl.bl_len = exlen;
147 bl.bl_data = exptr;
148 bl.bl_from_loclist = false;
149 bl.bl_section_offset = 0; // FAKE
150 return DW_DLV_OK;
151 }
152
153 static void
154 addSkipBranchOps(Dwarf_P_Debug dbg,
155 IRepresentation & Irep,
156 Dwarf_P_Die ourdie,
157 IRDie &inDie,
158 IRDie &inParent,
159 list<IRAttr>& attrs,
160 unsigned level)
161 {
162 static int done = false;
163
164 (void)Irep;
165 (void)ourdie;
166 (void)inParent;
167 (void)level;
168 if (!cmdoptions.addskipbranch) {
169 // No transformation of this sort requested.
170 return;
171 }
172 if (done) {
173 return;
174 }
175 Dwarf_Half dietag = inDie.getTag();
176 if (dietag != DW_TAG_variable) {
177 return;
178 }
179 for (list<IRAttr>::iterator it = attrs.begin();
180 it != attrs.end();
181 it++) {
182 IRAttr & attr = *it;
183 Dwarf_Half attrnum = attr.getAttrNum();
184 Dwarf_Form_Class formclass = attr.getFormClass();
185 if (attrnum == DW_AT_location) {
186 // Before DW5 there was no DW_FORM_CLASS_EXPRLOC,
187 // so we would have done this with a different
188 // test.
189 if (formclass != DW_FORM_CLASS_EXPRLOC) {
190 // Ignore this.
191 return;
192 }
193 IRForm*f = attr.getFormData();
194 IRFormBlock *f2 = dynamic_cast<IRFormBlock *>(f);
195 if (!f2) {
196 cerr << "ERROR Impossible IRFormBlock cast fails"
197 ", attrnum "
198 <<attrnum << endl;
199 break;
200 }
201
202 std::vector<unsigned char> vec= f2->getBlockData();
203 Dwarf_Block bl;
204 int res = createskipbranchblock(dbg,bl);
205 if (res != DW_DLV_OK) {
206 return;
207 }
208 f2->insertBlock(&bl);
209 done = true;
210 break;
211 }
212 }
213 }
214
215 // The first special transformation is converting DW_AT_high_pc
216 // from FORM_addr to an offset and we choose FORM_uleb
217 // The attrs ref passed in is (sometimes) used to generate
218 // a new attrs list for the caller.
219 static void
220 specialAttrTransformations(Dwarf_P_Debug dbg,
221 IRepresentation & Irep,
222 Dwarf_P_Die ourdie,
223 IRDie &inDie,
224 list<IRAttr>& attrs,
225 unsigned level)
226 {
227 (void)dbg;
228 (void)Irep;
229 (void)ourdie;
230 (void)level;
231
232 if (!cmdoptions.transformHighpcToConst) {
233 // No transformation of this sort requested.
234 return;
235 }
236 Dwarf_Half dietag = inDie.getTag();
237 if (dietag != DW_TAG_subprogram) {
238 return;
239 }
240 bool foundhipc= false;
241 bool foundlopc= false;
242 Dwarf_Addr lopcval = 0;
243 Dwarf_Addr hipcval = 0;
244 for (list<IRAttr>::iterator it = attrs.begin();
245 it != attrs.end();
246 it++) {
247 IRAttr & attr = *it;
248 Dwarf_Half attrnum = attr.getAttrNum();
249 Dwarf_Half attrform = attr.getFinalForm();
250 Dwarf_Form_Class formclass = attr.getFormClass();
251 if (attrnum == DW_AT_high_pc) {
252 if (attrform == DW_FORM_udata) {
253 // Already the right form for the test.
254 // Nothing to do.
255 return;
256 }
257 if (formclass != DW_FORM_CLASS_ADDRESS) {
258 return;
259 }
260 IRForm*f = attr.getFormData();
261 IRFormAddress *f2 = dynamic_cast<IRFormAddress *>(f);
262 if (!f2) {
263 cerr << "ERROR Impossible IRFormAddress cast fails"
264 ", attrnum "
265 <<attrnum << endl;
266 break;
267 }
268 hipcval = f2->getAddress();
269 foundhipc = true;
270 continue;
271 }
272 if (attrnum == DW_AT_low_pc) {
273 if (formclass != DW_FORM_CLASS_ADDRESS) {
274 return;
275 }
276 IRForm*f = attr.getFormData();
277 IRFormAddress *f2 = dynamic_cast<IRFormAddress *>(f);
278 if (!f2) {
279 cerr << "ERROR Impossible IRFormAddress cast Fails"
280 ", attrnum "
281 <<attrnum << endl;
282 break;
283 }
284
285 lopcval = f2->getAddress();
286 foundlopc = true;
287 continue;
288 }
289 continue;
290 }
291 if (!foundlopc || !foundhipc) {
292 return;
293 }
294 Dwarf_Addr hipcoffset = hipcval - lopcval;
295 // Now we create a revised attribute.
296 list<IRAttr> revisedattrs;
297 for (list<IRAttr>::iterator it = attrs.begin();
298 it != attrs.end();
299 it++) {
300 IRAttr & attr = *it;
301 Dwarf_Half attrnum = attr.getAttrNum();
302 if (attrnum == DW_AT_high_pc) {
303 // Here we want to create a constant form
304 // to test that a const high_pc works.
305 // This is new in DWARF5.
306 IRAttr attr2(attrnum,
307 DW_FORM_udata,
308 DW_FORM_udata);
309 attr2.setFormClass(DW_FORM_CLASS_CONSTANT);
310 IRFormConstant *f = new IRFormConstant(
311 DW_FORM_udata,
312 DW_FORM_udata,
313 DW_FORM_CLASS_CONSTANT,
314 IRFormConstant::UNSIGNED,
315 hipcoffset,
316 0);
317 attr2.setFormData(f);
318 revisedattrs.push_back(attr2);
319 // Avoid memoryleak
320 attr.dropFormData();
321 continue;
322 }
323 revisedattrs.push_back(attr);
324 continue;
325 }
326 attrs = std::move(revisedattrs);
327 }
328
329 /* Create a data16 data item out of nothing... */
330 static void
331 addData16DataItem(Dwarf_P_Debug dbg,
332 IRepresentation & Irep,
333 Dwarf_P_Die ourdie,
334 IRDie &inDie,
335 IRDie &inParent,
336 list<IRAttr>& attrs,
337 unsigned level)
338 {
339 static bool alreadydone = false;
340
341 (void)dbg;
342 (void)Irep;
343 (void)ourdie;
344 if (alreadydone) {
345 return;
346 }
347 if (!cmdoptions.adddata16) {
348 // No transformation of this sort requested.
349 return;
350 }
351 if (level < 2) {
352 return;
353 }
354
355 Dwarf_Half dietag = inDie.getTag();
356 Dwarf_Half parenttag = inParent.getTag();
357 if (dietag != DW_TAG_variable || parenttag != DW_TAG_subprogram) {
358 return;
359 }
360 list<IRAttr> revisedattrs;
361 for (list<IRAttr>::iterator it = attrs.begin();
362 it != attrs.end();
363 it++) {
364 IRAttr & attr = *it;
365 Dwarf_Half attrnum = attr.getAttrNum();
366 if (attrnum == DW_AT_name){
367 // Avoid memoryleak
368 attr.dropFormData();
369 continue;
370 }
371 if (attrnum == DW_AT_const_value){
372 // Avoid memoryleak
373 attr.dropFormData();
374 continue;
375 }
376 revisedattrs.push_back(attr);
377 }
378
379 // add two new attrs.
380 Dwarf_Half attrnum = DW_AT_name;
381 const char *attrname("vardata16");
382 IRAttr attr2(attrnum,
383 DW_FORM_string,
384 DW_FORM_string);
385 attr2.setFormClass(DW_FORM_CLASS_STRING);
386 IRFormString *f = new IRFormString();
387 f->setInitialForm(DW_FORM_string);
388 f->setFinalForm(DW_FORM_string);
389 f->setString(attrname);
390 attr2.setFormData(f);
391 revisedattrs.push_back(attr2);
392
393 Dwarf_Form_Data16 data16 = {
394 0x01,0x08,
395 0x02,0x07,
396 0x03,0x06,
397 0x04,0x05,
398 0x05,0x04,
399 0x06,0x03,
400 0x07,0x02,
401 0x08,0x01
402 };
403 IRAttr attrc(DW_AT_const_value, DW_FORM_data16,DW_FORM_data16);
404 attrc.setFormClass(DW_FORM_CLASS_CONSTANT);
405 IRFormConstant *fc = new IRFormConstant(
406 DW_FORM_data16,
407 DW_FORM_data16,
408 DW_FORM_CLASS_CONSTANT,
409 data16);
410 attrc.setFormData(fc);
411 revisedattrs.push_back(attrc);
412 attrs = std::move(revisedattrs);
413 alreadydone = true;
414 }
415
416 int testvals[3] = {
417 -2018,
418 -2019,
419 -2018
420 };
421 const char *testnames[3] = {
422 "myimplicitconst1",
423 "myimplicitconst2newabbrev",
424 "myimplicitconst3share1abbrev"
425 };
426
427 static void
428 addSUNfuncoffsets(Dwarf_P_Debug dbg ,
429 IRepresentation & Irep,
430 Dwarf_P_Die ourdie,
431 IRDie &inDie,
432 IRDie &inParent,
433 list<IRAttr>& attrs,
434 unsigned level)
435 {
436 (void)Irep;
437 (void)ourdie;
438 (void)inParent;
439 (void)level;
440 if (!cmdoptions.addSUNfuncoffsets) {
441 // No transformation of this sort requested.
442 return;
443 }
444 Dwarf_Half dietag = inDie.getTag();
445 if (dietag == DW_TAG_compile_unit) {
446 }
447 if (dietag != DW_TAG_compile_unit) {
448 return;
449 }
450 // add new attr.
451
452 Dwarf_Half attrnum = DW_AT_SUN_func_offsets;
453 IRFormBlock *f = new IRFormBlock();
454
455 Dwarf_Signed signar[5] = {-1,2000,-2500000000ll,60000000000ll,-2};
456 Dwarf_Unsigned block_len = 0;
457 void *block_ptr;
458
459 int res = dwarf_compress_integer_block_a(dbg,
460 5,signar, &block_len,&block_ptr,&error);
461 if (res == DW_DLV_ERROR) {
462 delete f;
463 cerr << " FAIL: Unable to generate via " <<
464 "dwarf_compress_integer_block_a: err " <<
465 dwarf_errmsg(error) << endl;
466 return;
467 } else if (res == DW_DLV_NO_ENTRY) {
468 cerr << " FAIL: NO_ENTRY impossible but got it" <<
469 " from dwarf_compress_integer_block_a: err " << endl;
470 return;
471 }
472 f->setInitialForm(DW_FORM_block);
473 f->setFinalForm(DW_FORM_block);
474
475 Dwarf_Block bl;
476 bl.bl_len = block_len;
477 bl.bl_data = block_ptr;
478 bl.bl_from_loclist = false;
479 bl.bl_section_offset = 0; // FAKE
480 f->insertBlock(&bl);
481
482 IRAttr attr1(attrnum,
483 DW_FORM_block,
484 DW_FORM_block);
485 attr1.setFormClass(DW_FORM_CLASS_BLOCK);
486 attr1.setFormData(f);
487 attrs.push_back(attr1);
488 }
489
490 static void
491 addImplicitConstItem(Dwarf_P_Debug dbg,
492 IRepresentation & Irep,
493 Dwarf_P_Die ourdie,
494 IRDie &inDie,
495 IRDie &inParent,
496 list<IRAttr>& attrs,
497 unsigned level)
498 {
499 static int alreadydone = 0;
500
501 (void)dbg;
502 (void)Irep;
503 (void)ourdie;
504 if (alreadydone > 2) {
505 // The limit here MUST be below the size of
506 // testvals[] and testnames[] above.
507 // Some abbrevs should match
508 // if the test case is right. Others not.
509 return;
510 }
511 if (!cmdoptions.addimplicitconst) {
512 // No transformation of this sort requested.
513 return;
514 }
515 if (level < 2) {
516 return;
517 }
518
519 Dwarf_Half dietag = inDie.getTag();
520 Dwarf_Half parenttag = inParent.getTag();
521 if (dietag != DW_TAG_variable || parenttag != DW_TAG_subprogram) {
522 return;
523 }
524 list<IRAttr> revisedattrs;
525 for (list<IRAttr>::iterator it = attrs.begin();
526 it != attrs.end();
527 it++) {
528 IRAttr & attr = *it;
529 Dwarf_Half attrnum = attr.getAttrNum();
530 if (attrnum == DW_AT_name){
531 // Avoid memory leak.
532 attr.dropFormData();
533 continue;
534 }
535 if (attrnum == DW_AT_const_value){
536 // Avoid memory leak.
537 attr.dropFormData();
538 continue;
539 }
540 revisedattrs.push_back(attr);
541 }
542
543 // add two new attrs.
544 Dwarf_Half attrnum = DW_AT_name;
545 const char *attrname(testnames[alreadydone]);
546 IRAttr attr2(attrnum,
547 DW_FORM_string,
548 DW_FORM_string);
549 attr2.setFormClass(DW_FORM_CLASS_STRING);
550 IRFormString *f = new IRFormString();
551 f->setInitialForm(DW_FORM_implicit_const);
552 f->setFinalForm(DW_FORM_implicit_const);
553 f->setString(attrname);
554
555 attr2.setFormData(f);
556 revisedattrs.push_back(attr2);
557
558 Dwarf_Signed myconstval = testvals[alreadydone];
559
560 IRAttr attrc(DW_AT_const_value,
561 DW_FORM_implicit_const,DW_FORM_implicit_const);
562 attrc.setFormClass(DW_FORM_CLASS_CONSTANT);
563 IRFormConstant *fc = new IRFormConstant(
564 DW_FORM_implicit_const,
565 DW_FORM_implicit_const,
566 DW_FORM_CLASS_CONSTANT,
567 IRFormConstant::SIGNED,
568 0,myconstval);
569 attrc.setFormData(fc);
570 revisedattrs.push_back(attrc);
571 attrs = std::move(revisedattrs);
572 ++alreadydone;
573 }
574
575 // Here we emit all the DIEs for a single Die and
576 // its children. When level == 0 the inDie is
577 // the CU die.
578 static Dwarf_P_Die
579 HandleOneDieAndChildren(Dwarf_P_Debug dbg,
580 IRepresentation &Irep,
581 IRCUdata &cu,
582 IRDie &inDie,
583 IRDie &inParent,
584 unsigned level)
585 {
586 list<IRDie>& children = inDie.getChildren();
587 // We create our target DIE first so we can link
588 // children to it, but add no content yet.
589 Dwarf_P_Die gendie = 0;
590 int res =dwarf_new_die_a(dbg,inDie.getTag(),NULL,NULL,
591 NULL,NULL,&gendie,&error);
592 if (res != DW_DLV_OK) {
593 cerr << "Die creation failure. "<< endl;
594 exit(1);
595 }
596 inDie.setGeneratedDie(gendie);
597
598 Dwarf_P_Die lastch = 0;
599 for ( list<IRDie>::iterator it = children.begin();
600 it != children.end();
601 it++) {
602 IRDie & ch = *it;
603 Dwarf_P_Die chp = HandleOneDieAndChildren(dbg,Irep,
604 cu,ch,inDie,level+1);
605 int res2 = 0;
606
607 if (lastch) {
608 // Link to right of earlier sibling.
609 res2 = dwarf_die_link_a(chp,NULL,NULL,lastch,NULL,&error);
610 } else {
611 // Link as first child.
612 res2 = dwarf_die_link_a(chp,gendie,NULL,NULL, NULL,
613 &error);
614 }
615 if (res2 != DW_DLV_OK) {
616 cerr << "Die link failure. "<< endl;
617 exit(1);
618 }
619 lastch = chp;
620 }
621 {
622 list<IRAttr>& attrs = inDie.getAttributes();
623
624 // Now any special transformations to the attrs list.
625 specialAttrTransformations(dbg,Irep,gendie,inDie,attrs,level);
626 addData16DataItem(dbg,Irep,gendie,inDie,inParent,attrs,level);
627 addImplicitConstItem(dbg,Irep,gendie,inDie,inParent,attrs,level);
628 addSUNfuncoffsets(dbg,Irep,gendie,inDie,inParent,attrs,level);
629 addSkipBranchOps( dbg,Irep,gendie,inDie,inParent,attrs,level);
630
631 // Now we add attributes (content), if any, to the
632 // output die 'gendie'.
633 for (list<IRAttr>::iterator it = attrs.begin();
634 it != attrs.end();
635 it++) {
636 IRAttr & attr = *it;
637
638 AddAttrToDie(dbg,Irep,cu,gendie,inDie,attr);
639 }
640 }
641 return gendie;
642 }
643
644 static void
645 HandleLineData(Dwarf_P_Debug dbg,
646 IRepresentation & Irep,
647 IRCUdata&cu)
648 {
649 Dwarf_Error lerror = 0;
650 // We refer to files by fileno, this builds an index.
651 pathToUnsignedType pathmap;
652
653 IRCULineData& ld = cu.getCULines();
654 std::vector<IRCULine> & cu_lines = ld.get_cu_lines();
655 //std::vector<IRCUSrcfile> &cu_srcfiles = ld.get_cu_srcfiles();
656
657 (void)Irep;
658 if (cu_lines.empty()) {
659 // No lines data to emit, do nothing.
660 return;
661 }
662 // To start with, we are doing a trivial generation here.
663 // To be refined 'soon'. FIXME
664 // Initially we don't worry about dwarf_add_directory_decl().
665
666 bool firstline = true;
667 bool addrsetincu = false;
668 for (unsigned k = 0; k < cu_lines.size(); ++k) {
669 IRCULine &li = cu_lines[k];
670 const std::string&path = li.getpath();
671 unsigned pathindex = 0;
672 pathToUnsignedType::const_iterator it = pathmap.find(path);
673
674 if (it == pathmap.end()) {
675 Dwarf_Error l2error = 0;
676 Dwarf_Unsigned idx = 0;
677 int res = dwarf_add_file_decl_a(
678 dbg,const_cast<char *>(path.c_str()),
679 0,0,0,&idx,&l2error);
680 if (res != DW_DLV_OK) {
681 cerr << "Error from dwarf_add_file_decl() on " <<
682 path << endl;
683 exit(1);
684 }
685 pathindex = idx;
686 pathmap[path] = pathindex;
687 } else {
688 pathindex = it->second;
689 }
690 Dwarf_Addr a = li.getaddr();
691 bool addrsetinline = li.getaddrset();
692 bool endsequence = li.getendsequence();
693 if (firstline || !addrsetincu) {
694 // We fake an elf sym index here.
695 Dwarf_Unsigned elfsymidx = 0;
696 if (firstline && !addrsetinline) {
697 cerr << "Error building line, "
698 "first entry not addr set" <<
699 endl;
700 exit(1);
701 }
702 int res = dwarf_lne_set_address_a(dbg,
703 a,elfsymidx,&lerror);
704 if (res != DW_DLV_OK) {
705 cerr << "Error building line, "
706 "dwarf_lne_set_address" <<
707 endl;
708 exit(1);
709 }
710 addrsetincu = true;
711 firstline = false;
712 } else if ( endsequence) {
713 int res = dwarf_lne_end_sequence_a(dbg,
714 a,&lerror);
715 if (res != DW_DLV_OK) {
716 cerr << "Error building line, "
717 "dwarf_lne_end_sequence" <<
718 endl;
719 exit(1);
720 }
721 addrsetincu = false;
722 continue;
723 }
724 Dwarf_Signed linecol = li.getlinecol();
725 // It's really the code address or (when in a proper compiler)
726 // a section or function offset.
727 // libdwarf subtracts the code_offset from the address passed
728 // this way or from dwarf_lne_set_address() and writes a small
729 // offset in a DW_LNS_advance_pc instruction.
730 Dwarf_Addr code_offset = a;
731 Dwarf_Unsigned lineno = li.getlineno();
732 Dwarf_Bool isstmt = li.getisstmt()?1:0;
733 Dwarf_Bool isblock = li.getisblock()?1:0;
734 Dwarf_Bool isepiloguebegin = li.getepiloguebegin()?1:0;
735 Dwarf_Bool isprologueend = li.getprologueend()?1:0;
736 Dwarf_Unsigned isa = li.getisa();
737 Dwarf_Unsigned discriminator = li.getdiscriminator();
738
739 int lires = dwarf_add_line_entry_c(dbg,
740 pathindex,
741 code_offset,
742 lineno,
743 linecol,
744 isstmt,
745 isblock,
746 isepiloguebegin,
747 isprologueend,
748 isa,
749 discriminator,
750 &lerror);
751 if (lires != DW_DLV_OK) {
752 cerr << "Error building line, dwarf_add_line_entry" <<
753 endl;
754 exit(1);
755 }
756 }
757 if (addrsetincu) {
758 cerr << "CU Lines did not end in an end_sequence!" << endl;
759 }
760 }
761 // This emits the DIEs for a single CU and possibly line data
762 // associated with the CU.
763 // The DIEs form a graph (which can be created and linked together
764 // in any order) and which is emitted in tree preorder as
765 // defined by the DWARF spec.
766 //
767 static void
768 emitOneCU( Dwarf_P_Debug dbg,IRepresentation & Irep,
769 IRCUdata&cu,
770 int cu_of_input_we_output)
771 {
772 // We descend the the tree, creating DIEs and linking
773 // them in as we return back up the tree of recursing
774 // on IRDie children.
775 Dwarf_Error lerror;
776
777 (void)cu_of_input_we_output;
778 IRDie & basedie = cu.baseDie();
779 Dwarf_P_Die cudie = HandleOneDieAndChildren(dbg,Irep,
780 cu,basedie,basedie,0);
781
782 // Add base die to debug, this is the CU die.
783 // This is not a good design as DWARF3/4 have
784 // requirements of multiple CUs in a single creation,
785 // which cannot be handled yet.
786 int res = dwarf_add_die_to_debug_a(dbg,cudie,&lerror);
787 if (res != DW_DLV_OK) {
788 cerr << "Unable to add_die_to_debug " << endl;
789 exit(1);
790 }
791
792 // Does fixup of IRFormReference targets.
793 cu.updateClassReferenceTargets();
794
795 HandleLineData(dbg,Irep,cu);
796 }
797
798 // .debug_info creation.
799 // Also creates .debug_line
800 static void
801 transform_debug_info(Dwarf_P_Debug dbg,
802 IRepresentation & irep,int cu_of_input_we_output)
803 {
804 int cu_number = 0;
805 std::list<IRCUdata> &culist = irep.infodata().getCUData();
806 // For now, just one CU we write (as spoken by Yoda).
807
808 for ( list<IRCUdata>::iterator it = culist.begin();
809 it != culist.end();
810 it++,cu_number++) {
811 if (cu_number == cu_of_input_we_output) {
812 IRCUdata & primecu = *it;
813 emitOneCU(dbg,irep,primecu,cu_of_input_we_output);
814 break;
815 }
816 }
817 }
818 static void
819 transform_debug_names(Dwarf_P_Debug dbg,
820 IRepresentation & irep,
821 int cu_of_input_we_output)
822 {
823 (void)dbg;
824 (void)irep;
825 (void)cu_of_input_we_output;;
826 #if 0
827 int cu_number = 0;
828 std::list<IRCUdata> &culist = irep.infodata().getCUData();
829 // For now, just one CU we write (as spoken by Yoda).
830
831 for ( list<IRCUdata>::iterator it = culist.begin();
832 it != culist.end();
833 it++,cu_number++) {
834 }
835 #endif
836 }
837
838 static void
839 transform_cie_fde(Dwarf_P_Debug dbg,
840 IRepresentation & Irep,
841 int cu_of_input_we_output)
842 {
843 Dwarf_Error err = 0;
844 std::vector<IRCie> &cie_vec =
845 Irep.framedata().get_cie_vec();
846 std::vector<IRFde> &fde_vec =
847 Irep.framedata().get_fde_vec();
848
849 (void)cu_of_input_we_output;
850 Dwarf_Unsigned cievecsize = cie_vec.size();
851 if (!cievecsize) {
852 // If debug_frame missing try for eh_frame.
853 // Just do one section, not both, for now.
854 cie_vec = Irep.ehframedata().get_cie_vec();
855 fde_vec = Irep.ehframedata().get_fde_vec();
856 cievecsize = cie_vec.size();
857 }
858 for (Dwarf_Unsigned i = 0; i < cievecsize ; ++i) {
859 IRCie &ciein = cie_vec[i];
860 Dwarf_Unsigned version = 0;
861 string aug;
862 Dwarf_Unsigned code_align = 0;
863 Dwarf_Signed data_align = 0;
864 Dwarf_Half ret_addr_reg = -1;
865 void * bytes = 0;
866 Dwarf_Unsigned bytes_len = 0;
867 Dwarf_Unsigned out_cie_index = 0;
868
869 ciein.get_basic_cie_data(&version, &aug,
870 &code_align, &data_align, &ret_addr_reg);
871 ciein.get_init_instructions(&bytes_len,&bytes);
872 // version implied: FIXME, need to let user code set output
873 // frame version.
874 char *str = const_cast<char *>(aug.c_str());
875 int res = dwarf_add_frame_cie_a(dbg, str,
876 code_align, data_align, ret_addr_reg,
877 bytes,bytes_len,
878 &out_cie_index,
879 &err);
880 if (res != DW_DLV_OK) {
881 cerr << "Error creating cie from input cie " << i << endl;
882 exit(1);
883 }
884 vector<int> fdeindex;
885 // This inner loop is C*F so a bit slow.
886 for (size_t j = 0; j < fde_vec.size(); ++j) {
887 IRFde &fdein = fde_vec[j];
888 Dwarf_Unsigned code_len = 0;
889 Dwarf_Addr code_virt_addr = 0;
890 Dwarf_Unsigned cie_input_index = 0;
891
892 fdein.get_fde_base_data(&code_virt_addr,
893 &code_len, &cie_input_index);
894 if (cie_input_index != i) {
895 // Wrong cie, ignore this fde right now.
896 continue;
897 }
898
899 Dwarf_P_Fde fdeout = 0;
900
901 res = dwarf_new_fde_a(dbg,&fdeout,&err);
902 if (res != DW_DLV_OK) {
903 cerr << "Error creating new fde " << j << endl;
904 exit(1);
905 }
906 Dwarf_Unsigned ilen = 0;
907 void *instrs = 0;
908 fdein.get_fde_instructions(&ilen, &instrs);
909
910 res = dwarf_insert_fde_inst_bytes(dbg,
911 fdeout, ilen, instrs,&err);
912 if (res != DW_DLV_OK) {
913 cerr << "Error inserting frame instr block " <<
914 j << endl;
915 exit(1);
916 }
917
918 Dwarf_P_Die irix_die = 0;
919 Dwarf_Signed irix_table_offset = 0;
920 Dwarf_Unsigned irix_excep_sym = 0;
921 Dwarf_Unsigned code_virt_addr_symidx =
922 Irep.getBaseTextSymbol();
923 Dwarf_Unsigned fde_index = 0;
924 Dwarf_Unsigned end_symbol_index = 0;
925 Dwarf_Unsigned offset_from_end_symbol = 0;
926
927 res = dwarf_add_frame_info_c(
928 dbg, fdeout,irix_die,
929 out_cie_index, code_virt_addr,
930 code_len,
931 code_virt_addr_symidx,
932 end_symbol_index,
933 offset_from_end_symbol,
934 irix_table_offset,irix_excep_sym,
935 &fde_index,
936 &err);
937 if (res != DW_DLV_OK) {
938 cerr << "Error creating new fde " << j << endl;
939 exit(1);
940 }
941 }
942 }
943 if (cmdoptions.addframeadvanceloc) {
944 // Add a whole new fde, cie, and some instructions
945 Dwarf_Unsigned code_align = 1;
946 Dwarf_Signed data_align = 1;
947 Dwarf_Half ret_addr_reg = 2; // fake, of course.
948 void * bytes = 0;
949 Dwarf_Unsigned bytes_len = 0;
950 Dwarf_Unsigned out_cie_index = 0;
951
952 const char *augstr = "";
953 int res = dwarf_add_frame_cie_a(dbg,
954 (char *)augstr,
955 code_align,
956 data_align,
957 ret_addr_reg,
958 bytes,bytes_len,
959 &out_cie_index,
960 &err);
961 if (res != DW_DLV_OK) {
962 cerr << "Error creating made-up addframeadvanceloc cie "
963 << endl;
964 exit(1);
965 }
966 Dwarf_P_Fde fdeout = 0;
967
968 res = dwarf_new_fde_a(dbg,&fdeout,&err);
969 if (res != DW_DLV_OK) {
970 cerr << "Error creating addframeadvance fde " << endl;
971 exit(1);
972 }
973 // These lead to a set of adv_loc ops values in the output
974 // for the fde which looks odd in -f
975 // output (-vvv -f makes more sense), might be
976 // better to have some additional frame instrs in there
977 // so plain -f looks more sensible.
978 std::list<Dwarf_Unsigned> adval;
979 adval.push_back(48);
980 adval.push_back(64);
981 adval.push_back(17219);
982 adval.push_back(4408131);
983 adval.push_back(18308350787ull);
984 // 0x30 0x40 0x4343 0x434343 0x434343434
985 unsigned i = 3;
986 for ( list<Dwarf_Unsigned>::iterator it =
987 adval.begin();
988 it != adval.end();it++ , ++i ) {
989
990 Dwarf_Unsigned v = *it;
991 res = dwarf_add_fde_inst_a(fdeout,
992 DW_CFA_advance_loc,v,0,&err);
993 if (res != DW_DLV_OK) {
994 cerr << "Error adding advance_loc" << v << endl;
995 exit(1);
996 }
997 res = dwarf_add_fde_inst_a(fdeout,
998 DW_CFA_same_value,i,0,&err);
999 if (res != DW_DLV_OK) {
1000 cerr << "Error adding dummy same_value op" <<
1001 v << endl;
1002 exit(1);
1003 }
1004 }
1005 // We increase code len to account for the
1006 // big advance_loc values inserted above.
1007 Dwarf_P_Die irix_die = 0;
1008 Dwarf_Signed irix_table_offset = 0;
1009 Dwarf_Unsigned irix_excep_sym = 0;
1010 Dwarf_Unsigned code_virt_addr_symidx =
1011 Irep.getBaseTextSymbol();
1012 Dwarf_Unsigned fde_index = 0;
1013 Dwarf_Unsigned end_symbol_index = 0;
1014 Dwarf_Unsigned offset_from_end_symbol = 0;
1015 Dwarf_Addr code_virt_addr = 0;
1016 Dwarf_Addr code_len = 0x444343434;
1017
1018 res = dwarf_add_frame_info_c(
1019 dbg, fdeout,irix_die,
1020 out_cie_index, code_virt_addr,
1021 code_len,
1022 code_virt_addr_symidx,
1023 end_symbol_index,
1024 offset_from_end_symbol,
1025 irix_table_offset,irix_excep_sym,
1026 &fde_index,
1027 &err);
1028 if (res != DW_DLV_OK) {
1029 cerr << "Error creating advance_loc fde " << endl;
1030 exit(1);
1031 }
1032 }
1033 }
1034
1035 static void
1036 transform_macro_info(Dwarf_P_Debug dbg,
1037 IRepresentation & Irep,
1038 int cu_of_input_we_output)
1039 {
1040 IRMacro &macrodata = Irep.macrodata();
1041 std::vector<IRMacroRecord> &macrov = macrodata.getMacroVec();
1042
1043 (void)cu_of_input_we_output;
1044 for (size_t m = 0; m < macrov.size() ; m++ ) {
1045 // FIXME: we need to coordinate with generated
1046 // CUs .
1047 cout << "FIXME: macros not really output yet " <<
1048 m << " " <<
1049 macrov.size() << endl;
1050 }
1051 Dwarf_Unsigned reloc_count = 0;
1052 int drd_version = 0;
1053 int res = dwarf_get_relocation_info_count(dbg,&reloc_count,
1054 &drd_version,&error);
1055 if ( res != DW_DLV_OK) {
1056 cerr << "Error getting relocation info count." << endl;
1057 exit(1);
1058
1059 }
1060 for (Dwarf_Unsigned ct = 0; ct < reloc_count ; ++ct) {
1061 }
1062 }
1063
1064 // Starting at a Die, look through its children
1065 // in its input to find which one we have by
1066 // comparing the input-die global offset.
1067 static
1068 Dwarf_P_Die findTargetDieByOffset(IRDie& indie,
1069 Dwarf_Unsigned targetglobaloff)
1070 {
1071 Dwarf_Unsigned globoff = indie.getGlobalOffset();
1072 if (globoff == targetglobaloff) {
1073 return indie.getGeneratedDie();
1074 }
1075 std::list<IRDie> dielist = indie.getChildren();
1076 for ( list<IRDie>::iterator it = dielist.begin();
1077 it != dielist.end();
1078 it++) {
1079 IRDie &ldie = *it;
1080 Dwarf_P_Die foundDie = findTargetDieByOffset(ldie,
1081 targetglobaloff);
1082 if (foundDie) {
1083 return foundDie;
1084 }
1085 }
1086 return NULL;
1087 }
1088
1089 // If the pubnames/pubtypes entry is in the
1090 // cu we are emitting, find the generated output
1091 // Dwarf_P_Die in that CU
1092 // and attach the pubname/type entry to it.
1093 static void
1094 transform_debug_pubnames_types_inner(Dwarf_P_Debug dbg,
1095 IRepresentation & Irep,
1096 int cu_of_input_we_output,
1097 IRCUdata&cu)
1098 {
1099 // First, get the target CU. */
1100 Dwarf_Unsigned targetcuoff= cu.getCUdieOffset();
1101 IRDie &basedie = cu.baseDie();
1102 IRPubsData& pubs = Irep.pubnamedata();
1103 std::list<IRPub> &nameslist = pubs.getPubnames();
1104
1105 (void)cu_of_input_we_output;
1106 if (!nameslist.empty()) {
1107 for ( list<IRPub>::iterator it = nameslist.begin();
1108 it != nameslist.end();
1109 it++) {
1110 IRPub &pub = *it;
1111 Dwarf_Unsigned pubcuoff= pub.getCUdieOffset();
1112 Dwarf_Unsigned ourdieoff= pub.getDieOffset();
1113 if (pubcuoff != targetcuoff) {
1114 continue;
1115 }
1116 Dwarf_P_Die targdie = findTargetDieByOffset(basedie,
1117 ourdieoff);
1118 if (targdie) {
1119 // Ugly. Old mistake in libdwarf declaration.
1120 char *mystr = const_cast<char *>
1121 (pub.getName().c_str());
1122 int res = dwarf_add_pubname_a(
1123 dbg,targdie,
1124 mystr,
1125 &error);
1126 if (res != DW_DLV_OK) {
1127 cerr << "Failed to add pubname entry for offset"
1128 << ourdieoff
1129 << "in CU at offset " << pubcuoff << endl;
1130 exit(1);
1131 }
1132 } else {
1133 cerr << "Did not find target pubname "
1134 "P_Die for offset "
1135 << ourdieoff
1136 << "in CU at offset " << pubcuoff << endl;
1137 }
1138 }
1139 }
1140 std::list<IRPub> &typeslist = pubs.getPubtypes();
1141 if (!typeslist.empty()) {
1142 for ( list<IRPub>::iterator it = typeslist.begin();
1143 it != typeslist.end();
1144 it++) {
1145 IRPub &pub = *it;
1146 Dwarf_Unsigned pubcuoff= pub.getCUdieOffset();
1147 Dwarf_Unsigned ourdieoff= pub.getDieOffset();
1148 if (pubcuoff != targetcuoff) {
1149 continue;
1150 }
1151 Dwarf_P_Die targdie = findTargetDieByOffset(basedie,
1152 ourdieoff);
1153 if (targdie) {
1154 // Ugly. Old mistake in libdwarf declaration.
1155 char *mystr = const_cast<char *>
1156 (pub.getName().c_str());
1157 int res = dwarf_add_pubtype_a(
1158 dbg,targdie,
1159 mystr,
1160 &error);
1161 if (!res) {
1162 cerr << "Failed to add pubtype entry for offset"
1163 << ourdieoff
1164 << "in CU at offset " << pubcuoff << endl;
1165 exit(1);
1166 }
1167 } else {
1168 cerr << "Did not find target pubtype "
1169 "P_Die for offset "
1170 << ourdieoff
1171 << "in CU at offset " << pubcuoff << endl;
1172 }
1173 }
1174 }
1175 }
1176
1177 // This looks for pubnames/pubtypes
1178 // to generate based on an object file input.
1179 static void
1180 transform_debug_pubnames_types(Dwarf_P_Debug dbg,
1181 IRepresentation & Irep,int cu_of_input_we_output)
1182 {
1183 int cu_number = 0;
1184 std::list<IRCUdata> &culist = Irep.infodata().getCUData();
1185 // For now, just one CU we write (as spoken by Yoda).
1186
1187 for ( list<IRCUdata>::iterator it = culist.begin();
1188 it != culist.end();
1189 it++,cu_number++) {
1190 if (cu_number == cu_of_input_we_output) {
1191 IRCUdata & primecu = *it;
1192 transform_debug_pubnames_types_inner(
1193 dbg,Irep,cu_of_input_we_output,primecu);
1194 break;
1195 }
1196 }
1197 }
1198
1199 void
1200 transform_irep_to_dbg(Dwarf_P_Debug dbg,
1201 IRepresentation & Irep,int cu_of_input_we_output)
1202 {
1203 transform_debug_info(dbg,Irep,cu_of_input_we_output);
1204 transform_cie_fde(dbg,Irep,cu_of_input_we_output);
1205 transform_macro_info(dbg,Irep,cu_of_input_we_output);
1206 transform_debug_pubnames_types(dbg,Irep,cu_of_input_we_output);
1207 transform_debug_names(dbg,Irep,cu_of_input_we_output);
1208 }