Debian Codegraph @

src/lib/libdwarfp/dwarf_pro_die.c

raw
1 /*
2
3 Copyright (C) 2000,2004 Silicon Graphics, Inc. All Rights Reserved.
4 Portions Copyright 2002-2010 Sun Microsystems, Inc. All rights reserved.
5 Portions Copyright 2011-2017 David Anderson. All Rights Reserved.
6
7 This program is free software; you can redistribute it
8 and/or modify it under the terms of version 2.1 of the
9 GNU Lesser General Public License as published by the Free
10 Software Foundation.
11
12 This program is distributed in the hope that it would be
13 useful, but WITHOUT ANY WARRANTY; without even the implied
14 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE.
16
17 Further, this software is distributed without any warranty
18 that it is free of the rightful claim of any third person
19 regarding infringement or the like. Any license provided
20 herein, whether implied or otherwise, applies only to this
21 software file. Patent licenses, if any, provided herein
22 do not apply to combinations of this program with other
23 software, or any other product whatsoever.
24
25 You should have received a copy of the GNU Lesser General
26 Public License along with this program; if not, write the
27 Free Software Foundation, Inc., 51 Franklin Street - Fifth
28 Floor, Boston MA 02110-1301, USA.
29
30 */
31
32 #include <config.h>
33
34 #include <stddef.h> /* NULL */
35 #include <stdlib.h> /* calloc() free() */
36 #include <string.h> /* memcpy() strcmp() strcpy() strlen() */
37
38 #ifdef HAVE_STDINT_H
39 #include <stdint.h> /* uintptr_t */
40 #endif
41
42 #include "dwarf.h"
43 #include "libdwarf.h"
44 #include "dwarf_base_types.h"
45 #include "libdwarfp.h"
46 #include "dwarf_pro_incl.h"
47 #include "dwarf_pro_opaque.h"
48 #include "dwarf_pro_error.h"
49 #include "dwarf_pro_util.h"
50 #include "dwarf_pro_alloc.h"
51 #include "dwarf_pro_die.h"
52 #include "dwarf_pro_section.h"
53 #include "dwarf_tsearch.h"
54
55 #ifndef R_MIPS_NONE
56 #define R_MIPS_NONE 0
57 #endif
58
59 /* This function creates a new die.
60 tag: tag of the new die to be created
61 parent,child,left,right: specify neighbors of the new die. Only
62 one of these may be non-null */
63 int
64 dwarf_new_die_a(Dwarf_P_Debug dbg,
65 Dwarf_Tag tag,
66 Dwarf_P_Die parent,
67 Dwarf_P_Die child,
68 Dwarf_P_Die left, Dwarf_P_Die right,
69 Dwarf_P_Die *die_out,
70 Dwarf_Error *error)
71 {
72 Dwarf_P_Die ret_die = 0;
73 int res = 0;
74
75 ret_die = (Dwarf_P_Die)
76 _dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_P_Die_s));
77 if (ret_die == NULL) {
78 DWARF_P_DBG_ERROR(dbg, DW_DLE_DIE_ALLOC,
79 DW_DLV_ERROR);
80 }
81 ret_die->di_parent = NULL;
82 ret_die->di_left = NULL;
83 ret_die->di_right = NULL;
84 ret_die->di_child = NULL;
85 ret_die->di_last_child = NULL;
86 ret_die->di_tag = tag;
87 ret_die->di_dbg = dbg;
88 ret_die->di_marker = 0;
89 res = dwarf_die_link_a(ret_die, parent, child, left, right,
90 error);
91 if (res != DW_DLV_OK) {
92 _dwarf_p_dealloc((Dwarf_Small *)ret_die);
93 ret_die = 0;
94 } else {
95 *die_out = ret_die;
96 }
97 return res;
98 }
99
100 /* This function links up a die to specified neighbors
101 parent,child,left,right: specify neighbors of the new die. Only
102 one of these may be non-null
103 This is the original version. Use dwarf_die_link_a()
104 instead as that function is easier to use (in checking for error).
105
106 New September 2016.
107 Error return easier to deal with
108 old version. */
109 int
110 dwarf_die_link_a(Dwarf_P_Die new_die,
111 Dwarf_P_Die parent,
112 Dwarf_P_Die child,
113 Dwarf_P_Die left, Dwarf_P_Die right, Dwarf_Error * error)
114 {
115 /* Count the # of non null neighbors. */
116 int n_nulls = 0;
117
118 if (parent != NULL) {
119 n_nulls++;
120 if (new_die->di_parent != NULL) {
121 DWARF_P_DBG_ERROR(NULL, DW_DLE_LINK_LOOP,
122 DW_DLV_ERROR);
123 }
124 new_die->di_parent = parent;
125 if (parent->di_child) {
126
127 /* di_last_child identifies the last sibling, the
128 die we want to attach new_die to. */
129 /* ASSERT: if di_child is set so is di_last_child. */
130 Dwarf_P_Die former_lastchild = parent->di_last_child;
131 parent->di_last_child = new_die;
132 /* Attach to the new die to end of the sibling list. */
133 former_lastchild->di_right = new_die;
134 new_die->di_left = former_lastchild;
135 } else {
136 parent->di_child = new_die;
137 parent->di_last_child = new_die;
138 }
139 }
140 if (child != NULL) {
141 n_nulls++;
142 new_die->di_child = child;
143 new_die->di_last_child = child;
144 if (child->di_parent) {
145 DWARF_P_DBG_ERROR(NULL, DW_DLE_PARENT_EXISTS,
146 DW_DLV_ERROR);
147 } else {
148 child->di_parent = new_die;
149 }
150 }
151 if (left != NULL) {
152 n_nulls++;
153 new_die->di_left = left;
154 if (left->di_right) {
155 /* There's already a right sibling of left,
156 insert the new die in the list. */
157 new_die->di_right = left->di_right;
158 left->di_right->di_left = new_die;
159 }
160 left->di_right = new_die;
161 if (new_die->di_parent) {
162 DWARF_P_DBG_ERROR(NULL, DW_DLE_PARENT_EXISTS,
163 DW_DLV_ERROR);
164 } else {
165 new_die->di_parent = left->di_parent;
166 }
167 }
168 if (right != NULL) {
169 n_nulls++;
170 new_die->di_right = right;
171 if (right->di_left) {
172 /* There is already a left sibling of the right die,
173 insert the new die in the list. */
174 new_die->di_left = right->di_left;
175 right->di_left->di_right = new_die;
176 }
177 right->di_left = new_die;
178 if (new_die->di_parent) {
179 DWARF_P_DBG_ERROR(NULL, DW_DLE_PARENT_EXISTS,
180 DW_DLV_ERROR);
181 } else {
182 new_die->di_parent = right->di_parent;
183 }
184 }
185 if (n_nulls > 1) {
186 /* Multiple neighbors! error! */
187 DWARF_P_DBG_ERROR(NULL, DW_DLE_EXTRA_NEIGHBORS,
188 DW_DLV_ERROR);
189 }
190 return DW_DLV_OK;
191 }
192
193 int
194 dwarf_add_die_marker_a(Dwarf_P_Debug dbg,
195 Dwarf_P_Die die,
196 Dwarf_Unsigned marker,
197 Dwarf_Error * error)
198 {
199 if (die == NULL) {
200 DWARF_P_DBG_ERROR(dbg, DW_DLE_DIE_NULL,
201 DW_DLV_ERROR);
202 }
203 die->di_marker = marker;
204 return DW_DLV_OK;
205 }
206
207 int
208 dwarf_get_die_marker_a(Dwarf_P_Debug dbg,
209 Dwarf_P_Die die,
210 Dwarf_Unsigned * marker,
211 Dwarf_Error * error)
212 {
213 if (die == NULL) {
214 DWARF_P_DBG_ERROR(dbg, DW_DLE_DIE_NULL,
215 DW_DLV_ERROR);
216 }
217 *marker = die->di_marker;
218 return DW_DLV_ERROR;
219 }
220
221 /*---------------------------------------------------------
222 This function adds a die to dbg struct. It should
223 be called using the root of all the dies.
224 ---------------------------------------------------------*/
225 /* New September 2016. The new and preferred form. */
226 int
227 dwarf_add_die_to_debug_a(Dwarf_P_Debug dbg,
228 Dwarf_P_Die first_die, Dwarf_Error * error)
229 {
230 if (first_die == NULL) {
231 DWARF_P_DBG_ERROR(dbg, DW_DLE_DIE_NULL,
232 DW_DLV_ERROR);
233 }
234 if (first_die->di_tag != DW_TAG_compile_unit) {
235 DWARF_P_DBG_ERROR(dbg, DW_DLE_WRONG_TAG,
236 DW_DLV_ERROR);
237 }
238 dbg->de_dies = first_die;
239 return DW_DLV_OK;
240 }
241
242 int
243 _dwarf_pro_add_AT_stmt_list(Dwarf_P_Debug dbg,
244 Dwarf_P_Die first_die, Dwarf_Error * error)
245 {
246 Dwarf_P_Attribute new_attr;
247 int uwordb_size = dbg->de_dwarf_offset_size;
248
249 /* Add AT_stmt_list attribute */
250 new_attr = (Dwarf_P_Attribute)
251 _dwarf_p_get_alloc(dbg,
252 sizeof(struct Dwarf_P_Attribute_s));
253 if (new_attr == NULL) {
254 DWARF_P_DBG_ERROR(NULL, DW_DLE_ATTR_ALLOC,
255 DW_DLV_ERROR);
256 }
257
258 new_attr->ar_attribute = DW_AT_stmt_list;
259 new_attr->ar_attribute_form =
260 dbg->de_ar_data_attribute_form;
261 new_attr->ar_rel_type = dbg->de_offset_reloc;
262
263 new_attr->ar_nbytes = uwordb_size;
264 new_attr->ar_next = NULL;
265 new_attr->ar_reloc_len = uwordb_size;
266 new_attr->ar_data = (char *)
267 _dwarf_p_get_alloc(dbg, uwordb_size);
268 if (new_attr->ar_data == NULL) {
269 DWARF_P_DBG_ERROR(NULL,DW_DLE_ADDR_ALLOC,
270 DW_DLV_ERROR);
271 }
272 {
273 Dwarf_Unsigned du = 0;
274
275 WRITE_UNALIGNED(dbg, (void *) new_attr->ar_data,
276 (const void *) &du, sizeof(du), uwordb_size);
277 }
278
279 _dwarf_pro_add_at_to_die(first_die, new_attr);
280 return DW_DLV_OK;
281 }
282
283 static int
284 _dwarf_debug_str_compare_func(const void *l,const void *r)
285 {
286 const struct Dwarf_P_debug_str_entry_s*el = l;
287 const struct Dwarf_P_debug_str_entry_s*er = r;
288 char *lname = 0;
289 char *rname = 0;
290 int ir = 0;
291
292 if (el->dse_has_table_offset) {
293 /* When set the name is in the debug_str table. */
294 /* ASSERT: dse_dbg->de_debug_str->ds_data
295 is non-zero.
296 ASSERT: dse_name NULL. */
297 lname = el->dse_dbg->de_debug_str->ds_data +
298 el->dse_table_offset;
299 } else {
300 /* ASSERT: dse_name non-null */
301 lname = el->dse_name;
302 }
303 if (er->dse_has_table_offset) {
304 /* When set the name is in the debug_str table. */
305 /* ASSERT: dse_dbg->de_debug_str->ds_data
306 is non-zero.
307 ASSERT: dse_name NULL. */
308 rname = er->dse_dbg->de_debug_str->ds_data +
309 er->dse_table_offset;
310 } else {
311 /* ASSERT: dse_name non-null */
312 rname = er->dse_name;
313 }
314 ir = strcmp(lname,rname);
315 return ir;
316 }
317
318 static void
319 debug_str_entry_free_func(void *m)
320 {
321 free(m);
322 }
323
324 static int
325 make_debug_str_entry(Dwarf_P_Debug dbg,
326 struct Dwarf_P_debug_str_entry_s **mt_out,
327 char *name,
328 unsigned slen,
329 unsigned char has_offset,
330 Dwarf_Unsigned offset_in_table,
331 Dwarf_Error *error)
332 {
333 struct Dwarf_P_debug_str_entry_s *mt =
334 (struct Dwarf_P_debug_str_entry_s *)calloc(
335 sizeof(struct Dwarf_P_debug_str_entry_s),1);
336 if (!mt) {
337 _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
338 return DW_DLV_ERROR;
339 }
340
341 mt->dse_slen = slen;
342 mt->dse_table_offset = 0;
343 mt->dse_dbg = dbg;
344 if (has_offset) {
345 mt->dse_has_table_offset = TRUE;
346 mt->dse_table_offset = offset_in_table;
347 mt->dse_name = 0;
348 } else {
349 /* ASSERT: name != NULL */
350 mt->dse_has_table_offset = FALSE;
351 /* We just set dse_table_offset so it has
352 a known value, though nothing should refer
353 to dse_table_offset because
354 dse_has_table_offset is FALSE.*/
355 mt->dse_table_offset = 0;
356 mt->dse_name = name;
357 }
358 *mt_out = mt;
359 return DW_DLV_OK;
360 }
361 #define STRTAB_BASE_ALLOC_SIZE 2048
362 static int
363 insert_debug_str_data_string(Dwarf_P_Debug dbg,
364 char *name,
365 unsigned slen,
366 Dwarf_P_Section_Data sd,
367 Dwarf_Unsigned*adding_at_offset,
368 Dwarf_Error * error)
369 {
370 Dwarf_Unsigned current_offset = 0;
371
372 if (!sd->ds_data) {
373 Dwarf_Unsigned initial_alloc = STRTAB_BASE_ALLOC_SIZE;
374 Dwarf_Unsigned base_insert_offset = 0;
375
376 /* Inserting our first string.
377 The GNU linker refuses to commonize strings
378 if the section starts with a NUL byte,
379 so start with real string, using a
380 base_insert_offset of 0. */
381 if ( (slen + base_insert_offset) >= STRTAB_BASE_ALLOC_SIZE) {
382 initial_alloc = slen *2+ base_insert_offset;
383 }
384 if (initial_alloc < slen) {
385 _dwarf_p_error(dbg, error, DW_DLE_SIZE_WRAPAROUND);
386 return DW_DLV_ERROR;
387 }
388 sd->ds_data = calloc(1,initial_alloc);
389 if (!sd->ds_data) {
390 _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
391 return DW_DLV_ERROR;
392 }
393 sd->ds_orig_alloc = initial_alloc;
394 *adding_at_offset = base_insert_offset;
395 sd->ds_nbytes = slen + base_insert_offset;
396 strcpy(sd->ds_data+base_insert_offset,name);
397 return DW_DLV_OK;
398 }
399 current_offset = sd->ds_nbytes;
400 if ( (current_offset + slen) >= sd->ds_orig_alloc) {
401 unsigned updated_length = sd->ds_orig_alloc;
402 char *newbuf = 0;
403 if (slen > updated_length) {
404 /* Very long string passed in. */
405 updated_length = slen *2;
406 } else {
407 updated_length = updated_length *2;
408 }
409 if (updated_length < sd->ds_orig_alloc) {
410 _dwarf_p_error(dbg, error, DW_DLE_SIZE_WRAPAROUND);
411 return DW_DLV_ERROR;
412 }
413 newbuf = calloc(1,updated_length);
414 if (!newbuf) {
415 _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
416 return DW_DLV_ERROR;
417 }
418 memcpy(newbuf,sd->ds_data,sd->ds_nbytes);
419 free(sd->ds_data);
420 sd->ds_data = newbuf;
421 sd->ds_orig_alloc = updated_length;
422 newbuf = 0;
423 }
424 strcpy(sd->ds_data + current_offset,name);
425 sd->ds_nbytes += slen;
426 *adding_at_offset = current_offset;
427 return DW_DLV_OK;
428 }
429
430 /* Find the string offset using the hash table,
431 and if not known, insert the new string. */
432 int
433 _dwarf_insert_or_find_in_debug_str(Dwarf_P_Debug dbg,
434 char *name,
435 enum dwarf_which_hash whash,
436 unsigned slen, /* includes space for trailing NUL */
437 Dwarf_Unsigned *offset_in_debug_str,
438 Dwarf_Error *error)
439 {
440 struct Dwarf_P_debug_str_entry_s *mt = 0;
441 struct Dwarf_P_debug_str_entry_s *mt2 = 0;
442 struct Dwarf_P_debug_str_entry_s *retval = 0;
443 struct Dwarf_P_debug_str_entry_s *re = 0;
444 int res = 0;
445 Dwarf_Unsigned adding_at_offset = 0;
446 void **hashtab = 0;
447 Dwarf_P_Section_Data sd = 0;
448 struct Dwarf_P_Str_stats_s * stats = 0;
449
450 switch (whash) {
451 case _dwarf_hash_debug_str:
452 hashtab = &dbg->de_debug_str_hashtab;
453 sd = dbg->de_debug_str;
454 stats = &dbg->de_stats.ps_strp;
455 break;
456 case _dwarf_hash_debug_line_str:
457 hashtab = &dbg->de_debug_line_str_hashtab;
458 sd = dbg->de_debug_line_str;
459 stats = &dbg->de_stats.ps_line_strp;
460 break;
461 case _dwarf_hash_debug_str_sup:
462 default:
463 /* Not supported or unknown. */
464 _dwarf_p_error(dbg, error,
465 DW_DLE_STRING_HASHTAB_IDENTITY_ERROR);
466 return DW_DLV_ERROR;
467 }
468 res = make_debug_str_entry(dbg,&mt,name,
469 slen,FALSE, 0, error);
470 if (res != DW_DLV_OK) {
471 return res;
472 }
473 /* We do a find as we do not want the string pointer passed in
474 to be in the hash table, we want a pointer into the
475 debug_str table in the hash table. */
476 retval = dwarf_tfind(mt,(void *const*)hashtab,
477 _dwarf_debug_str_compare_func);
478 if (retval) {
479
480 stats->ps_strp_reused_count++;
481 stats->ps_strp_reused_len += slen;
482
483 re = *(struct Dwarf_P_debug_str_entry_s **)retval;
484 *offset_in_debug_str = re->dse_table_offset;
485 debug_str_entry_free_func(mt);
486 return DW_DLV_OK;
487 }
488
489 /* We know the string is not in .debug_str data yet.
490 Insert it into the big string table and get that
491 offset. */
492
493 debug_str_entry_free_func(mt);
494 mt = 0;
495 res = insert_debug_str_data_string(dbg,name,slen,sd,
496 &adding_at_offset, error);
497 if (res != DW_DLV_OK) {
498 return res;
499 }
500
501 /* The name is in the string table itself, so use that pointer
502 and offset for the string. */
503 res = make_debug_str_entry(dbg,&mt2, 0,
504 slen,TRUE,adding_at_offset,error);
505 if (res != DW_DLV_OK) {
506 return res;
507 }
508 retval = dwarf_tsearch(mt2,
509 (void *)hashtab,
510 _dwarf_debug_str_compare_func);
511 if (!retval) {
512 debug_str_entry_free_func(mt2);
513 _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
514 return DW_DLV_ERROR;
515 }
516
517 /* This indirection is one of the surprises in using tsearch... */
518 re = *(struct Dwarf_P_debug_str_entry_s **)retval;
519 if (re != mt2) {
520 debug_str_entry_free_func(mt2);
521 /* Found it in hash tab: illogical as the tsearch_find should
522 have found it. */
523 _dwarf_p_error(dbg, error, DW_DLE_ILLOGICAL_TSEARCH);
524 return DW_DLV_ERROR;
525 }
526 stats->ps_strp_count_debug_str++;
527 stats->ps_strp_len_debug_str += slen;
528 /* we added it to hash, do not free mt2 (which == re). */
529 *offset_in_debug_str = re->dse_table_offset;
530 return DW_DLV_OK;
531 }
532
533 /* Returns DW_DLV_OK or DW_DLV_ERROR. */
534 int _dwarf_pro_set_string_attr(Dwarf_P_Attribute new_attr,
535 Dwarf_P_Debug dbg,
536 char *name,
537 Dwarf_Error *error)
538 {
539 int form = dbg->de_debug_default_str_form;
540 unsigned slen = strlen(name)+1;
541
542 if (form == DW_FORM_string ||
543 slen <= dbg->de_dwarf_offset_size) {
544 new_attr->ar_nbytes = slen;
545 new_attr->ar_next = 0;
546
547 new_attr->ar_data =
548 (char *) _dwarf_p_get_alloc(dbg, slen);
549 if (new_attr->ar_data == NULL) {
550 _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
551 return DW_DLV_ERROR;
552 }
553 dbg->de_stats.ps_str_count++;
554 dbg->de_stats.ps_str_total_length += slen;
555
556 strcpy(new_attr->ar_data, name);
557 new_attr->ar_attribute_form = DW_FORM_string;
558 new_attr->ar_rel_type = R_MIPS_NONE;
559 new_attr->ar_reloc_len = 0; /* unused for R_MIPS_NONE */
560 return DW_DLV_OK;
561 }
562 if (form == DW_FORM_strp) {
563 int uwordb_size = dbg->de_dwarf_offset_size;
564 Dwarf_Unsigned offset_in_debug_str = 0;
565 int res = 0;
566
567 res = _dwarf_insert_or_find_in_debug_str(dbg,name,
568 _dwarf_hash_debug_str,slen,
569 &offset_in_debug_str,error);
570 if (res != DW_DLV_OK) {
571 return res;
572 }
573 new_attr->ar_attribute_form = form;
574 new_attr->ar_rel_type = dbg->de_offset_reloc;
575 new_attr->ar_nbytes = uwordb_size;
576 new_attr->ar_next = NULL;
577 new_attr->ar_reloc_len = uwordb_size;
578 /* During transform to disk
579 a symbol index will be applied. */
580 new_attr->ar_data = (char *)
581 _dwarf_p_get_alloc(dbg, uwordb_size);
582 if (new_attr->ar_data == NULL) {
583 _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
584 return DW_DLV_ERROR;
585 }
586 {
587 Dwarf_Unsigned du = offset_in_debug_str;
588
589 WRITE_UNALIGNED(dbg, (void *) new_attr->ar_data,
590 (const void *) &du, sizeof(du), uwordb_size);
591 }
592
593 return DW_DLV_OK;
594 }
595 _dwarf_p_error(dbg, error, DW_DLE_BAD_STRING_FORM);
596 return DW_DLV_ERROR;
597
598 }
599
600 /*------------------------------------------------------------------
601 Add AT_name attribute to die
602 --------------------------------------------------------------------*/
603 /* New December 2018. */
604 int
605 dwarf_add_AT_name_a(Dwarf_P_Die die, char *name,
606 Dwarf_P_Attribute *newattr_out,
607 Dwarf_Error * error)
608 {
609 Dwarf_P_Attribute new_attr = 0;
610 int res = 0;
611
612 if (die == NULL) {
613 DWARF_P_DBG_ERROR(NULL, DW_DLE_DIE_NULL,
614 DW_DLV_ERROR);
615 }
616 new_attr = (Dwarf_P_Attribute)
617 _dwarf_p_get_alloc(die->di_dbg,
618 sizeof(struct Dwarf_P_Attribute_s));
619 if (new_attr == NULL) {
620 DWARF_P_DBG_ERROR(NULL, DW_DLE_ATTR_ALLOC,
621 DW_DLV_ERROR);
622 }
623
624 /* fill in the information */
625 new_attr->ar_attribute = DW_AT_name;
626 res = _dwarf_pro_set_string_attr(new_attr,die->di_dbg,name,error);
627 if (res != DW_DLV_OK) {
628 return DW_DLV_ERROR;
629 }
630
631 /* add attribute to the die */
632 _dwarf_pro_add_at_to_die(die, new_attr);
633 *newattr_out = new_attr;
634 return DW_DLV_OK;
635 }
636
637 /*--------------------------------------------------------------------
638 Add AT_comp_dir attribute to die
639 --------------------------------------------------------------------*/
640 int
641 dwarf_add_AT_comp_dir_a(Dwarf_P_Die ownerdie,
642 char *current_working_directory,
643 Dwarf_P_Attribute *attr_out,
644 Dwarf_Error * error)
645 {
646 Dwarf_P_Attribute new_attr = 0;
647 int res = 0;
648
649 if (ownerdie == NULL) {
650 DWARF_P_DBG_ERROR(NULL, DW_DLE_DIE_NULL,
651 DW_DLV_ERROR);
652 }
653 new_attr = (Dwarf_P_Attribute)
654 _dwarf_p_get_alloc(ownerdie->di_dbg,
655 sizeof(struct Dwarf_P_Attribute_s));
656 if (new_attr == NULL) {
657 DWARF_P_DBG_ERROR(NULL, DW_DLE_ATTR_ALLOC,
658 DW_DLV_ERROR);
659 }
660
661 /* fill in the information */
662 new_attr->ar_attribute = DW_AT_comp_dir;
663 res = _dwarf_pro_set_string_attr(new_attr,ownerdie->di_dbg,
664 current_working_directory,error);
665 if (res != DW_DLV_OK) {
666 return res;
667 }
668
669 /* add attribute to the die */
670 _dwarf_pro_add_at_to_die(ownerdie, new_attr);
671 *attr_out = new_attr;
672 return DW_DLV_OK;
673 }
674
675 int
676 _dwarf_pro_add_AT_fde(Dwarf_P_Debug dbg,
677 Dwarf_P_Die die,
678 Dwarf_Unsigned offset, Dwarf_Error * error)
679 {
680 Dwarf_P_Attribute new_attr;
681 int uwordb_size = dbg->de_dwarf_offset_size;
682
683 if (die == NULL) {
684 DWARF_P_DBG_ERROR(NULL, DW_DLE_DIE_NULL, DW_DLV_ERROR);
685 }
686 new_attr = (Dwarf_P_Attribute)
687 _dwarf_p_get_alloc(dbg,sizeof(struct Dwarf_P_Attribute_s));
688 if (new_attr == NULL) {
689 DWARF_P_DBG_ERROR(NULL, DW_DLE_ATTR_ALLOC, DW_DLV_ERROR);
690 }
691
692 /* fill in the information */
693 new_attr->ar_attribute = DW_AT_MIPS_fde;
694 new_attr->ar_attribute_form = dbg->de_ar_data_attribute_form;
695 new_attr->ar_rel_type = dbg->de_offset_reloc;
696 new_attr->ar_nbytes = uwordb_size;
697 new_attr->ar_next = NULL;
698 new_attr->ar_reloc_len = uwordb_size;
699 new_attr->ar_data = (char *)
700 _dwarf_p_get_alloc(dbg, uwordb_size);
701 if (new_attr->ar_data == NULL) {
702 DWARF_P_DBG_ERROR(NULL, DW_DLE_ADDR_ALLOC, DW_DLV_ERROR);
703 }
704 {
705 Dwarf_Unsigned du = offset;
706
707 WRITE_UNALIGNED(dbg, (void *) new_attr->ar_data,
708 (const void *) &du, sizeof(du), uwordb_size);
709 }
710 _dwarf_pro_add_at_to_die(die, new_attr);
711 return DW_DLV_OK;
712 }
713
714 /* Sept 2016: returns DW_DLV_OK or DW_DLV_ERROR */
715 int
716 _dwarf_pro_add_AT_macro_info(Dwarf_P_Debug dbg,
717 Dwarf_P_Die die,
718 Dwarf_Unsigned offset, Dwarf_Error * error)
719 {
720 Dwarf_P_Attribute new_attr;
721 int uwordb_size = dbg->de_dwarf_offset_size;
722
723 if (die == NULL) {
724 DWARF_P_DBG_ERROR(NULL, DW_DLE_DIE_NULL, DW_DLV_ERROR);
725 }
726 new_attr = (Dwarf_P_Attribute)
727 _dwarf_p_get_alloc(dbg,sizeof(struct Dwarf_P_Attribute_s));
728 if (new_attr == NULL) {
729 DWARF_P_DBG_ERROR(NULL, DW_DLE_ATTR_ALLOC, DW_DLV_ERROR);
730 }
731
732 /* fill in the information */
733 new_attr->ar_attribute = DW_AT_macro_info;
734 new_attr->ar_attribute_form = dbg->de_ar_data_attribute_form;
735 new_attr->ar_rel_type = dbg->de_offset_reloc;
736
737 new_attr->ar_nbytes = uwordb_size;
738 new_attr->ar_next = NULL;
739 new_attr->ar_reloc_len = uwordb_size;
740 new_attr->ar_data = (char *)
741 _dwarf_p_get_alloc(dbg, uwordb_size);
742 if (new_attr->ar_data == NULL) {
743 DWARF_P_DBG_ERROR(NULL, DW_DLE_ADDR_ALLOC, DW_DLV_ERROR);
744 }
745 {
746 Dwarf_Unsigned du = offset;
747
748 WRITE_UNALIGNED(dbg, (void *) new_attr->ar_data,
749 (const void *) &du, sizeof(du), uwordb_size);
750 }
751
752 _dwarf_pro_add_at_to_die(die, new_attr);
753
754 return DW_DLV_OK;
755 }
756
757 /* Updates the list of attributes on this Dwarf_P_Die
758 */
759 void
760 _dwarf_pro_add_at_to_die(Dwarf_P_Die die, Dwarf_P_Attribute attr)
761 {
762 if (die->di_last_attr) {
763 /* Inserts new attr at the end */
764 die->di_last_attr->ar_next = attr;
765 die->di_last_attr = attr;
766 die->di_n_attr++;
767 } else {
768 die->di_n_attr = 1;
769 die->di_attrs = die->di_last_attr = attr;
770 }
771 }