Debian Codegraph @

src/lib/libdwarfp/dwarf_pro_section.c

raw
1 /*
2 Copyright (C) 2000,2004,2006 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright (C) 2007-2019 David Anderson. All Rights Reserved.
4 Portions Copyright 2002-2010 Sun Microsystems, Inc. All rights reserved.
5 Portions Copyright 2012 SN Systems Ltd. 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> /* free() malloc() qsort() */
36 #include <string.h> /* memcpy() strcmp() strcpy() strlen() */
37
38 #include "dwarf.h"
39 #include "libdwarf.h"
40 #include "dwarf_base_types.h"
41 #include "libdwarfp.h"
42 #include "dwarf_pro_incl.h"
43 #include "dwarf_pro_opaque.h"
44 #include "dwarf_pro_error.h"
45 #include "dwarf_pro_util.h"
46 #include "dwarf_pro_alloc.h"
47 #include "dwarf_pro_section.h"
48 #include "dwarf_pro_line.h"
49 #include "dwarf_pro_frame.h"
50 #include "dwarf_pro_die.h"
51 #include "dwarf_pro_macinfo.h"
52 #include "dwarf_pro_types.h"
53 #include "dwarf_pro_dnames.h"
54
55 struct Dwarf_Sort_Abbrev_s {
56 Dwarf_Unsigned dsa_attr;
57 Dwarf_Unsigned dsa_form;
58 Dwarf_Signed dsa_implicitvalue;
59 Dwarf_P_Attribute dsa_attrp;
60 };
61
62 /* Must match up with pro_section.h defines of DEBUG_INFO etc
63 and sectnames (below). REL_SEC_PREFIX is either ".rel" or ".rela"
64 see pro_incl.h
65 */
66 const char *_dwarf_rel_section_names[] = {
67 REL_SEC_PREFIX ".debug_info",
68 REL_SEC_PREFIX ".debug_line",
69
70 /* Nothing here refers to anything. */
71 REL_SEC_PREFIX ".debug_abbrev",
72
73 REL_SEC_PREFIX ".debug_frame",
74 REL_SEC_PREFIX ".debug_aranges",
75 REL_SEC_PREFIX ".debug_pubnames",
76 REL_SEC_PREFIX ".debug_funcnames", /* sgi extension */
77 REL_SEC_PREFIX ".debug_typenames", /* sgi extension */
78 REL_SEC_PREFIX ".debug_varnames", /* sgi extension */
79 REL_SEC_PREFIX ".debug_weaknames", /* sgi extension */
80 REL_SEC_PREFIX ".debug_macinfo",
81 REL_SEC_PREFIX ".debug_loc",
82 REL_SEC_PREFIX ".debug_ranges",
83 REL_SEC_PREFIX ".debug_types", /* new in DWARF4 */
84 REL_SEC_PREFIX ".debug_pubtypes", /* new in DWARF3 */
85 REL_SEC_PREFIX ".debug_names", /* DWARF5 aka dnames */
86
87 /* Nothing here refers to anything.*/
88 REL_SEC_PREFIX ".debug_str",
89
90 REL_SEC_PREFIX ".debug_rnglists", /* DWARF5. */
91
92 /* DWARF5. Nothing referselsewhere */
93 REL_SEC_PREFIX ".debug_line_str",
94
95 REL_SEC_PREFIX ".debug_macro", /* DWARF5. */
96 REL_SEC_PREFIX ".debug_loclists", /* DWARF5. */
97 REL_SEC_PREFIX ".debug_rnglists", /* DWARF5. */
98 REL_SEC_PREFIX ".debug_sup", /* DWARF5. No relocs on this */
99 };
100
101 /* names of sections. Ensure that it matches the defines
102 in pro_section.h, in the same order
103 Must match also _dwarf_rel_section_names above
104 */
105 const char *_dwarf_sectnames[] = {
106 ".debug_info",
107 ".debug_line",
108 ".debug_abbrev",
109 ".debug_frame",
110 ".debug_aranges",
111 ".debug_pubnames",
112 ".debug_funcnames", /* sgi extension */
113 ".debug_typenames", /* sgi extension */
114 ".debug_varnames", /* sgi extension */
115 ".debug_weaknames", /* sgi extension */
116 ".debug_macinfo",
117 ".debug_loc",
118 ".debug_ranges",
119 ".debug_types", /* new in DWARF4 */
120 ".debug_pubtypes", /* new in DWARF3 */
121 ".debug_names", /* new in DWARF5. aka dnames */
122 ".debug_str",
123 ".debug_line_str", /* new in DWARF5 */
124 ".debug_macro", /* new in DWARF5 */
125 ".debug_loclists", /* new in DWARF5 */
126 ".debug_rnglists", /* new in DWARF5 */
127 ".debug_sup", /* new in DWARF5 */
128 };
129
130 static const Dwarf_Ubyte std_opcode_len[] = { 0, /* DW_LNS_copy */
131 1, /* DW_LNS_advance_pc */
132 1, /* DW_LNS_advance_line */
133 1, /* DW_LNS_set_file */
134 1, /* DW_LNS_set_column */
135 0, /* DW_LNS_negate_stmt */
136 0, /* DW_LNS_set_basic_block */
137 0, /* DW_LNS_const_add_pc */
138 1, /* DW_LNS_fixed_advance_pc */
139 /* The following for DWARF3 and DWARF4, though GNU
140 uses these in DWARF2 as well. */
141 0, /* DW_LNS_set_prologue_end */
142 0, /* DW_LNS_set_epilogue_begin */
143 1, /* DW_LNS_set_isa */
144 };
145
146 /* struct to hold relocation entries. Its maintained as a linked
147 list of relocation structs, and will then be written at as a
148 whole into the relocation section. Whether its 32 bit or
149 64 bit will be obtained from Dwarf_Debug pointer.
150 */
151
152 typedef struct Dwarf_P_Rel_s *Dwarf_P_Rel;
153 struct Dwarf_P_Rel_s {
154 Dwarf_P_Rel dr_next;
155 void *dr_rel_datap;
156 };
157 typedef struct Dwarf_P_Rel_Head_s *Dwarf_P_Rel_Head;
158 struct Dwarf_P_Rel_Head_s {
159 struct Dwarf_P_Rel_s *drh_head;
160 struct Dwarf_P_Rel_s *drh_tail;
161 };
162
163 static int _dwarf_pro_generate_debug_line_str(Dwarf_P_Debug dbg,
164 Dwarf_Unsigned *nbufs, Dwarf_Error * error);
165 static int _dwarf_pro_generate_dnames(Dwarf_P_Debug dbg,
166 Dwarf_Unsigned *nbufs, Dwarf_Error * error);
167 static int _dwarf_pro_generate_debug_str(Dwarf_P_Debug dbg,
168 Dwarf_Unsigned *nbufs, Dwarf_Error * error);
169 static int _dwarf_pro_generate_debugline(Dwarf_P_Debug dbg,
170 Dwarf_Unsigned *nbufs, Dwarf_Error * error);
171 static int _dwarf_pro_generate_debugframe(Dwarf_P_Debug dbg,
172 Dwarf_Unsigned *nbufs, Dwarf_Error * error);
173 static int _dwarf_pro_generate_debuginfo(Dwarf_P_Debug dbg,
174 Dwarf_Unsigned *nbufs, Dwarf_Error * error);
175 static int _dwarf_pro_generate_debugsup(Dwarf_P_Debug dbg,
176 Dwarf_Unsigned *nbufs, Dwarf_Error * error);
177
178 #if 0
179 static void
180 dump_bytes(char * msg,Dwarf_Small * start, long len)
181 {
182 Dwarf_Small *end = start + len;
183 Dwarf_Small *cur = start;
184
185 printf("%s len %ld ",msg,len);
186 for (; cur < end; cur++) {
187 printf("%02x ", *cur);
188 }
189 printf("\n");
190 }
191 #endif
192
193 #if 0
194 static void
195 print_single_abbrev(Dwarf_P_Abbrev c, unsigned idx)
196 {
197 unsigned j = 0;
198
199 printf(" %2u idx %2u tag 0x%x attrct %2u\n",idx,
200 (unsigned)c->abb_idx,
201 (unsigned)c->abb_tag,
202 (unsigned)c->abb_n_attr);
203
204 for ( ; j < (unsigned)c->abb_n_attr; ++j) {
205 printf(" %2u attr 0x%2x form 0x%2x impl val %"
206 DW_PR_DSd "\n",
207 j,
208 (unsigned)c->abb_attrs[j],
209 (unsigned)c->abb_forms[j]);
210 (unsigned)c->abb_implicits[j]);
211 }
212 }
213 static void
214 print_curabbrev(const char *where,
215 Dwarf_P_Abbrev curabbrev)
216 {
217 Dwarf_P_Abbrev ca = 0;
218 unsigned i = 0;
219 for (ca = curabbrev; ca ; ca = ca->abb_next,++i) {
220 printf("ABBREV %u from %s\n",i,where);
221 print_single_abbrev(ca,i);
222 }
223 }
224 #endif
225
226 /* These macros used as return value for _dwarf_pro_get_opc. */
227 #define OPC_INCS_ZERO -1
228 #define OPC_OUT_OF_RANGE -2
229 #define LINE_OUT_OF_RANGE -3
230 /* Given address advance and line advance, it gives
231 either special opcode, or a number < 0
232
233 FIXME: Check all three negative values.
234 Are any negatives really hard errors?
235 */
236 static int
237 _dwarf_pro_get_opc(
238 struct Dwarf_P_Line_Inits_s *inits,
239 Dwarf_Unsigned addr_adv,
240 int line_adv)
241 {
242 int line_base = inits->pi_line_base;
243 int line_range =inits->pi_line_range;
244 Dwarf_Unsigned factored_adv = 0;
245
246 factored_adv = addr_adv / inits->pi_minimum_instruction_length;
247 if (line_adv == 0 && factored_adv == 0) {
248 return OPC_INCS_ZERO;
249 }
250 if (line_adv >= line_base && line_adv < line_base + line_range) {
251 int opc = 0;
252
253 opc = (line_adv - line_base) +
254 (factored_adv * line_range) +
255 inits->pi_opcode_base;
256 if (opc > 255) {
257 return OPC_OUT_OF_RANGE;
258 }
259 return opc;
260 }
261 return LINE_OUT_OF_RANGE;
262 }
263
264 /* OFFSET_PLUS_EXTENSION_SIZE is the size of the
265 'length' field in total.
266 Which may be 4,8, or 12 bytes!
267 4 is standard DWARF2.
268 8 is non-standard MIPS-IRIX 64-bit.
269 12 is standard DWARF3 for 64 bit offsets.
270 Used in various routines: local variable names
271 must match the names here.
272 */
273 #define OFFSET_PLUS_EXTENSION_SIZE (offset_size + extension_size)
274
275 /* Return TRUE if we need the section, FALSE otherwise
276
277 If any of the 'line-data-related' calls were made
278 including file or directory entries,
279 produce .debug_line .
280
281 */
282 static int
283 dwarf_need_debug_line_section(Dwarf_P_Debug dbg)
284 {
285 if (dbg->de_output_version > 4) {
286 return FALSE;
287 }
288 if (dbg->de_lines == NULL && dbg->de_file_entries == NULL
289 && dbg->de_inc_dirs == NULL) {
290 return FALSE;
291 }
292 return TRUE;
293 }
294
295 /* DWARF5 only. */
296 static int
297 dwarf_need_debug_names_section(Dwarf_P_Debug dbg)
298 {
299 if (dbg->de_output_version < 5) {
300 return FALSE;
301 }
302 if (!dbg->de_dnames) {
303 return FALSE;
304 }
305 return dbg->de_force_dnames;
306 }
307
308 /* Convert debug information to a format such that
309 it can be written on disk.
310 Called exactly once per execution.
311 */
312 int
313 dwarf_transform_to_disk_form_a(Dwarf_P_Debug dbg,
314 Dwarf_Unsigned *count,
315 Dwarf_Error * error)
316 {
317 /* Section data in written out in a number of buffers. Each
318 _generate_*() function returns a cumulative
319 count of buffers for
320 all the sections.
321 dwarf_get_section_bytes() returns pointers to these
322 buffers one at a time. */
323 Dwarf_Unsigned nbufs = 0;
324 int sect = 0;
325 int err = 0;
326 Dwarf_Unsigned du = 0;
327
328 if (dbg->de_version_magic_number != PRO_VERSION_MAGIC) {
329 DWARF_P_DBG_ERROR(dbg, DW_DLE_IA, DW_DLV_ERROR);
330 }
331
332 /* Create dwarf section headers */
333 for (sect = 0; sect < NUM_DEBUG_SECTIONS; sect++) {
334 long flags = 0;
335
336 switch (sect) {
337 case DEBUG_SUP:
338 if (!dbg->de_debug_sup.ds_version) {
339 continue;
340 }
341 break;
342
343 case DEBUG_INFO:
344 if (dbg->de_dies == NULL) {
345 continue;
346 }
347 break;
348
349 case DEBUG_LINE:
350 if (dwarf_need_debug_line_section(dbg) == FALSE) {
351 continue;
352 }
353 break;
354
355 case DEBUG_ABBREV:
356 if (dbg->de_dies == NULL) {
357 continue;
358 }
359 break;
360
361 case DEBUG_FRAME:
362 if (dbg->de_frame_cies == NULL) {
363 continue;
364 }
365 flags = SHF_MIPS_NOSTRIP;
366 break;
367
368 case DEBUG_ARANGES:
369 if (dbg->de_arange == NULL) {
370 continue;
371 }
372 break;
373
374 case DEBUG_PUBNAMES:
375 if (dbg->de_simple_name_headers[dwarf_snk_pubname].
376 sn_head == NULL) {
377 continue;
378 }
379 break;
380 case DEBUG_PUBTYPES:
381 if (dbg->de_simple_name_headers[dwarf_snk_pubtype].
382 sn_head == NULL) {
383 continue;
384 }
385 break;
386
387 case DEBUG_STR:
388 if (dbg->de_debug_str->ds_data == NULL) {
389 continue;
390 }
391 break;
392
393 case DEBUG_FUNCNAMES:
394 if (dbg->de_simple_name_headers[dwarf_snk_funcname].
395 sn_head == NULL) {
396 continue;
397 }
398 break;
399
400 case DEBUG_TYPENAMES:
401 if (dbg->de_simple_name_headers[dwarf_snk_typename].
402 sn_head == NULL) {
403 continue;
404 }
405 break;
406
407 case DEBUG_VARNAMES:
408 if (dbg->de_simple_name_headers[dwarf_snk_varname].
409 sn_head == NULL) {
410 continue;
411 }
412 break;
413
414 case DEBUG_WEAKNAMES:
415 if (dbg->de_simple_name_headers[dwarf_snk_weakname].
416 sn_head == NULL) {
417 continue;
418 }
419 break;
420
421 case DEBUG_MACINFO:
422 if (dbg->de_first_macinfo == NULL) {
423 continue;
424 }
425 break;
426 case DEBUG_NAMES: /* DWARF5 */
427 if (dwarf_need_debug_names_section(dbg) == FALSE) {
428 continue;
429 }
430 break;
431 case DEBUG_LOC:
432 /* Not handled yet. */
433 continue;
434 case DEBUG_RANGES:
435 /* Not handled yet. */
436 continue;
437 case DEBUG_TYPES:
438 /* Not handled yet. */
439 continue;
440 case DEBUG_MACRO:
441 /* Not handled yet. */
442 continue;
443 case DEBUG_LOCLISTS:
444 /* Not handled yet. */
445 continue;
446 case DEBUG_RNGLISTS:
447 /* Not handled yet. */
448 continue;
449 case DEBUG_LINE_STR:
450 if (dwarf_need_debug_line_section(dbg) == FALSE) {
451 continue;
452 }
453 /* Not handled yet. */
454 continue;
455 default:
456 /* logic error: missing a case */
457 DWARF_P_DBG_ERROR(dbg, DW_DLE_ELF_SECT_ERR, DW_DLV_ERROR);
458 }
459 {
460 int new_base_elf_sect = 0;
461
462 if (dbg->de_callback_func) {
463 new_base_elf_sect =
464 dbg->de_callback_func(_dwarf_sectnames[sect],
465 /* rec size */ 1,
466 SECTION_TYPE,
467 flags, SHN_UNDEF, 0, &du,
468 dbg->de_user_data, &err);
469 }
470 if (new_base_elf_sect == -1) {
471 DWARF_P_DBG_ERROR(dbg, DW_DLE_ELF_SECT_ERR,
472 DW_DLV_ERROR);
473 }
474 dbg->de_elf_sects[sect] = new_base_elf_sect;
475 dbg->de_sect_name_idx[sect] = du;
476 }
477 }
478
479 nbufs = 0;
480
481 /* Changing the order in which the sections
482 are generated may cause
483 problems because of relocations. */
484
485 if (dbg->de_debug_sup.ds_version) {
486 int res = _dwarf_pro_generate_debugsup(dbg,
487 &nbufs, error);
488 if (res == DW_DLV_ERROR) {
489 return res;
490 }
491 }
492
493 if (dwarf_need_debug_line_section(dbg) == TRUE) {
494 int res = _dwarf_pro_generate_debugline(dbg,&nbufs, error);
495 if (res == DW_DLV_ERROR) {
496 return res;
497 }
498 }
499
500 if (dbg->de_frame_cies) {
501 int res = _dwarf_pro_generate_debugframe(dbg,&nbufs,error);
502 if (res == DW_DLV_ERROR) {
503 return res;
504 }
505 }
506 if (dbg->de_first_macinfo) {
507 /* For DWARF 2,3,4 only */
508 /* Need new code for DWARF5 macro info. FIXME*/
509 int res = _dwarf_pro_transform_macro_info_to_disk(dbg,
510 &nbufs,error);
511 if (res == DW_DLV_ERROR) {
512 return res;
513 }
514 }
515
516 if (dbg->de_dies) {
517 int res= _dwarf_pro_generate_debuginfo(dbg, &nbufs, error);
518 if (res == DW_DLV_ERROR) {
519 return res;
520 }
521 }
522
523 if (dbg->de_debug_str->ds_data) {
524 int res = _dwarf_pro_generate_debug_str(dbg,&nbufs, error);
525 if (res == DW_DLV_ERROR) {
526 return res;
527 }
528 }
529 if (dbg->de_debug_line_str->ds_data) {
530 int res = _dwarf_pro_generate_debug_line_str(dbg,&nbufs,
531 error);
532 if (res == DW_DLV_ERROR) {
533 return res;
534 }
535 }
536
537 if (dbg->de_arange) {
538 int res = _dwarf_transform_arange_to_disk(dbg,&nbufs, error);
539 if (res == DW_DLV_ERROR) {
540 return res;
541 }
542 }
543 if (dbg->de_output_version < 5) {
544 if (dbg->de_simple_name_headers[dwarf_snk_pubname].sn_head) {
545 int res = _dwarf_transform_simplename_to_disk(dbg,
546 dwarf_snk_pubname,
547 DEBUG_PUBNAMES,
548 &nbufs,
549 error);
550 if (res == DW_DLV_ERROR) {
551 return res;
552 }
553 }
554 if (dbg->de_simple_name_headers[dwarf_snk_pubtype].sn_head) {
555 int res = _dwarf_transform_simplename_to_disk(dbg,
556 dwarf_snk_pubtype,
557 DEBUG_PUBTYPES,
558 &nbufs,
559 error);
560 if (res == DW_DLV_ERROR) {
561 return res;
562 }
563 }
564
565 if (dbg->de_simple_name_headers[dwarf_snk_funcname].sn_head) {
566 int res = _dwarf_transform_simplename_to_disk(dbg,
567 dwarf_snk_funcname,
568 DEBUG_FUNCNAMES,
569 &nbufs,
570 error);
571 if (res == DW_DLV_ERROR) {
572 return res;
573 }
574 }
575
576 if (dbg->de_simple_name_headers[dwarf_snk_typename].sn_head) {
577 int res = _dwarf_transform_simplename_to_disk(dbg,
578 dwarf_snk_typename,
579 DEBUG_TYPENAMES,
580 &nbufs,
581 error);
582 if (res == DW_DLV_ERROR) {
583 return res;
584 }
585 }
586
587 if (dbg->de_simple_name_headers[dwarf_snk_varname].sn_head) {
588 int res = _dwarf_transform_simplename_to_disk(dbg,
589 dwarf_snk_varname,
590 DEBUG_VARNAMES,
591 &nbufs,
592 error);
593 if (res == DW_DLV_ERROR) {
594 return res;
595 }
596 }
597
598 if (dbg->de_simple_name_headers[dwarf_snk_weakname].sn_head) {
599 int res = _dwarf_transform_simplename_to_disk(dbg,
600 dwarf_snk_weakname, DEBUG_WEAKNAMES,
601 &nbufs,
602 error);
603 if (res == DW_DLV_ERROR) {
604 return res;
605 }
606 }
607 }
608 if (dwarf_need_debug_names_section(dbg) == TRUE) {
609 int res = _dwarf_pro_generate_dnames(dbg,&nbufs,
610 error);
611 if (res == DW_DLV_ERROR) {
612 return res;
613 }
614 }
615 #if 0 /* FIXME: TODO new sections */
616 if (dwarf_need_debug_macro_section(dbg) == TRUE) {
617 int res = _dwarf_pro_generate_debug_macro(dbg,&nbufs, error);
618 if (res == DW_DLV_ERROR) {
619 return res;
620 }
621 }
622 if (dwarf_need_debug_loclists_section(dbg) == TRUE) {
623 int res = _dwarf_pro_generate_debug_loclists(dbg,
624 &nbufs, error);
625 if (res == DW_DLV_ERROR) {
626 return res;
627 }
628 }
629 if (dwarf_need_debug_rnglists_section(dbg) == TRUE) {
630 int res = _dwarf_pro_generate_debug_rnglists(dbg,&nbufs,
631 error);
632 if (res == DW_DLV_ERROR) {
633 return res;
634 }
635 }
636 #endif
637
638 {
639 Dwarf_Signed new_chunks = 0;
640 int res = 0;
641
642 res = dbg->de_transform_relocs_to_disk(dbg, &new_chunks);
643 if (res != DW_DLV_OK) {
644 DWARF_P_DBG_ERROR(dbg, DW_DLE_RELOCS_ERROR,
645 DW_DLV_ERROR);
646 }
647 nbufs += new_chunks;
648 }
649 *count = nbufs;
650 return DW_DLV_OK;
651 }
652
653 static int
654 write_fixed_size(Dwarf_Unsigned val,
655 Dwarf_P_Debug dbg,
656 int elfsectno,
657 Dwarf_Unsigned size,
658 unsigned * size_out,
659 Dwarf_Error* error)
660 {
661 unsigned char *data = 0;
662 GET_CHUNK_ERR(dbg, elfsectno, data, size, error);
663 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &val,
664 sizeof(val), size);
665 *size_out = size;
666 return DW_DLV_OK;
667 }
668
669 static int
670 write_ubyte(unsigned val,
671 Dwarf_P_Debug dbg,
672 int elfsectno,
673 unsigned *len_out,
674 Dwarf_Error* error)
675 {
676 Dwarf_Ubyte db = val;
677 unsigned char *data = 0;
678 unsigned len = sizeof(Dwarf_Ubyte);
679 GET_CHUNK_ERR(dbg, elfsectno, data,
680 len, error);
681 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
682 sizeof(db), len);
683 *len_out = 1;
684 return DW_DLV_OK;;
685 }
686 static int
687 pretend_write_uval(Dwarf_Unsigned val,
688 Dwarf_P_Debug dbg,
689 unsigned *uval_len_out,
690 Dwarf_Error* error)
691 {
692 char buff1[ENCODE_SPACE_NEEDED];
693 int nbytes = 0;
694 int res = 0;
695
696 res = dwarf_encode_leb128(val,
697 &nbytes, buff1,
698 sizeof(buff1));
699 if (res != DW_DLV_OK) {
700 DWARF_P_DBG_ERROR(dbg,DW_DLE_LEB_OUT_ERROR , DW_DLV_ERROR);
701 }
702 *uval_len_out = nbytes;
703 return DW_DLV_OK;
704 }
705
706 static int
707 write_sval(Dwarf_Signed val,
708 Dwarf_P_Debug dbg,
709 int elfsectno,
710 unsigned *sval_len_out,
711 Dwarf_Error* error)
712 {
713 char buff1[ENCODE_SPACE_NEEDED];
714 unsigned char *data = 0;
715 int nbytes = 0;
716 int res = dwarf_encode_signed_leb128(val,
717 &nbytes, buff1,
718 sizeof(buff1));
719 if (res != DW_DLV_OK) {
720 DWARF_P_DBG_ERROR(dbg, DW_DLE_LEB_OUT_ERROR, DW_DLV_ERROR);
721 }
722 GET_CHUNK(dbg, elfsectno, data, nbytes, error);
723 memcpy((void *) data, (const void *) buff1, nbytes);
724 *sval_len_out = nbytes;
725 return DW_DLV_OK;
726 }
727
728 /* This one does not allocate a chunk, uses
729 an already existing chunk.
730 data points into that existing chunk. */
731 static int
732 append_uval(Dwarf_Unsigned val,
733 Dwarf_P_Debug dbg,
734 unsigned char *data,
735 unsigned * uval_len_out,
736 Dwarf_Error* error)
737 {
738 char buff1[ENCODE_SPACE_NEEDED];
739 int nbytes = 0;
740 int res = dwarf_encode_leb128(val,
741 &nbytes, buff1,
742 sizeof(buff1));
743 if (res != DW_DLV_OK) {
744 DWARF_P_DBG_ERROR(dbg, DW_DLE_LEB_OUT_ERROR, DW_DLV_ERROR);
745 }
746 memcpy((void *) data, (const void *) buff1, nbytes);
747 *uval_len_out = nbytes;
748 return DW_DLV_OK;
749 }
750
751 static int
752 write_uval(Dwarf_Unsigned val,
753 Dwarf_P_Debug dbg,
754 int elfsectno,
755 unsigned * uval_len_out,
756 Dwarf_Error* error)
757 {
758 char buff1[ENCODE_SPACE_NEEDED];
759 unsigned char *data = 0;
760 int nbytes = 0;
761 int res = dwarf_encode_leb128(val,
762 &nbytes, buff1,
763 sizeof(buff1));
764 if (res != DW_DLV_OK) {
765 DWARF_P_DBG_ERROR(dbg, DW_DLE_LEB_OUT_ERROR, DW_DLV_ERROR);
766 }
767 GET_CHUNK_ERR(dbg, elfsectno, data, nbytes, error);
768 memcpy((void *) data, (const void *) buff1, nbytes);
769 *uval_len_out = nbytes;
770 return DW_DLV_OK;
771 }
772
773 static unsigned
774 write_opcode_uval(int opcode,
775 Dwarf_P_Debug dbg,
776 int elfsectno,
777 Dwarf_Unsigned val,
778 unsigned *len_out,
779 Dwarf_Error* error)
780 {
781 unsigned ublen = 0;
782 int res = 0;
783 unsigned uvlen = 0;
784 res = write_ubyte(opcode,dbg,elfsectno,&ublen,error);
785 if (res != DW_DLV_OK) {
786 DWARF_P_DBG_ERROR(dbg, DW_DLE_LEB_OUT_ERROR, DW_DLV_ERROR);
787 }
788 res = write_uval(val,dbg,elfsectno,&uvlen,error);
789 if (res != DW_DLV_OK) {
790 DWARF_P_DBG_ERROR(dbg, DW_DLE_LEB_OUT_ERROR, DW_DLV_ERROR);
791 }
792 *len_out = ublen +uvlen;
793 return DW_DLV_OK;
794 }
795
796 static int
797 determine_form_size(Dwarf_P_Debug dbg,
798 unsigned format_count,
799 struct Dwarf_P_Line_format_s *format,
800 unsigned *size_out,
801 Dwarf_Bool write_out,
802 unsigned char *data,
803 Dwarf_Error *error)
804 {
805 unsigned calculated_size = 0;
806 unsigned n = 0;
807 int res = 0;
808
809 /* entry format itself */
810 calculated_size += sizeof_ubyte(dbg);
811
812 /* Space for the format details. */
813 for (n = 0; n < format_count; ++n) {
814 struct Dwarf_P_Line_format_s *lf = format+n;
815 unsigned val_len = 0;
816 unsigned val_len2 = 0;
817
818 if (write_out) {
819 res = append_uval(lf->def_content_type, dbg,
820 data,
821 &val_len,error);
822 } else {
823 res = pretend_write_uval(lf->def_content_type, dbg,
824 &val_len,error);
825 }
826 data += val_len;
827 if (res != DW_DLV_OK) {
828 return res;
829 }
830 if (write_out) {
831 res = append_uval(lf->def_form_code, dbg,
832 data,
833 &val_len2,error);
834 } else {
835 res = pretend_write_uval(lf->def_form_code, dbg,
836 &val_len2,error);
837 }
838 if (res != DW_DLV_OK) {
839 return res;
840 }
841 data += val_len2;
842 calculated_size += val_len + val_len2;
843 }
844 *size_out = calculated_size;
845 return DW_DLV_OK;
846 }
847
848 static int
849 determine_file_content_size(Dwarf_P_Debug dbg,
850 Dwarf_P_F_Entry entry_list,
851 Dwarf_Unsigned format_count,
852 struct Dwarf_P_Line_format_s *format,
853 unsigned *size_out,
854 Dwarf_Bool write_out,
855 unsigned char *data,
856 Dwarf_Error *error)
857 {
858 unsigned calculated_size = 0;
859 unsigned count_len = 0;
860 Dwarf_P_F_Entry cur = 0;
861 Dwarf_P_F_Entry nxt = 0;
862 int res = 0;
863 Dwarf_Unsigned offset_size = 0;
864
865 offset_size = dbg->de_dwarf_offset_size;
866 res = pretend_write_uval(format_count,dbg,
867 &count_len,error);
868 if (res != DW_DLV_OK) {
869 return res;
870 }
871 calculated_size += count_len;
872
873 cur = entry_list;
874 for ( ; cur; cur = nxt) {
875 unsigned f = 0;
876 nxt = cur->dfe_next;
877
878 for ( ; f < format_count; f++) {
879 struct Dwarf_P_Line_format_s *lf = format+f;
880 unsigned ctype = lf->def_content_type;
881 unsigned cform = lf->def_form_code;
882
883 switch (ctype) {
884 case DW_LNCT_path: {
885 switch(cform) {
886 case DW_FORM_string: {
887 unsigned slen = strlen(cur->dfe_name) +1;
888 calculated_size += slen;
889 if (write_out) {
890 strcpy((char *)data, cur->dfe_name);
891 data += slen;
892 }
893 }
894 break;
895 case DW_FORM_strp: {
896 unsigned slen = strlen(cur->dfe_name) +1;
897 if (write_out) {
898 Dwarf_Unsigned stroffset = 0;
899 res = _dwarf_insert_or_find_in_debug_str(
900 dbg,
901 cur->dfe_name,
902 _dwarf_hash_debug_str,
903 slen,
904 &stroffset,error);
905 if (res != DW_DLV_OK) {
906 return res;
907 }
908 WRITE_UNALIGNED(dbg, (void *) data,
909 (const void *) &stroffset,
910 sizeof(stroffset), offset_size);
911 data += offset_size;
912 }
913 calculated_size += offset_size;
914 }
915 break;
916 case DW_FORM_line_strp: {
917 unsigned slen = strlen(cur->dfe_name) +1;
918 if (write_out) {
919 Dwarf_Unsigned stroffset = 0;
920 res = _dwarf_insert_or_find_in_debug_str(
921 dbg,
922 cur->dfe_name,
923 _dwarf_hash_debug_line_str,
924 slen,
925 &stroffset,error);
926 if (res != DW_DLV_OK) {
927 return res;
928 }
929 WRITE_UNALIGNED(dbg, (void *) data,
930 (const void *) &stroffset,
931 sizeof(stroffset), offset_size);
932 data += offset_size;
933 }
934 calculated_size += offset_size;
935 }
936 break;
937 case DW_FORM_strp_sup:
938 /* Following in dwo only. */
939 case DW_FORM_strx:
940 case DW_FORM_strx1:
941 case DW_FORM_strx2:
942 case DW_FORM_strx3:
943 case DW_FORM_strx4:
944 default:
945 DWARF_P_DBG_ERROR(dbg,
946 DW_DLE_LNCT_FORM_CODE_NOT_HANDLED,
947 DW_DLV_ERROR);
948 break;
949 }
950 }
951 break;
952 case DW_LNCT_directory_index: {
953 switch(cform) {
954 case DW_FORM_data1:
955 calculated_size += 1;
956 if (write_out) {
957 unsigned char ub = cur->dfe_index;
958 *data = ub;
959 data += 1;
960 }
961 break;
962 case DW_FORM_data2:
963 calculated_size += DWARF_HALF_SIZE;
964 if (write_out) {
965 Dwarf_Half uh = cur->dfe_index;
966 memcpy(data,&uh,DWARF_HALF_SIZE);
967 data += DWARF_HALF_SIZE;
968 }
969 break;
970 case DW_FORM_udata: {
971 unsigned val_len = 0;
972 if (write_out) {
973 res = append_uval(cur->dfe_index,
974 dbg,
975 data,
976 &val_len,error);
977 data += val_len;
978 } else {
979 res = pretend_write_uval(cur->dfe_index,
980 dbg, &val_len,error);
981 }
982 if (res != DW_DLV_OK) {
983 return res;
984 }
985 calculated_size += val_len;
986 }
987 break;
988 default:
989 DWARF_P_DBG_ERROR(dbg,
990 DW_DLE_LNCT_FORM_CODE_NOT_HANDLED,
991 DW_DLV_ERROR);
992 }
993 }
994 break;
995 case DW_LNCT_timestamp: {
996 switch(cform) {
997 case DW_FORM_udata: {
998 unsigned val_len = 0;
999 if (write_out) {
1000 res = append_uval(cur->dfe_timestamp,
1001 dbg,
1002 data,
1003 &val_len,error);
1004 data += val_len;
1005 } else {
1006 res = pretend_write_uval(cur->dfe_timestamp,
1007 dbg, &val_len,error);
1008 }
1009 if (res != DW_DLV_OK) {
1010 return res;
1011 }
1012 calculated_size += val_len;
1013 }
1014 break;
1015 case DW_FORM_data4: {
1016 calculated_size += DWARF_32BIT_SIZE;
1017 if (write_out) {
1018 ASNOUT(data,cur->dfe_timestamp,
1019 DWARF_32BIT_SIZE);
1020 data += DWARF_32BIT_SIZE;
1021 }
1022 }
1023 break;
1024 case DW_FORM_data8:
1025 /* As of 2017 there is no 8 byte timestamp
1026 defined, though it does have to happen.
1027 before 2038. */
1028 calculated_size += DWARF_64BIT_SIZE;
1029 if (write_out) {
1030 Dwarf_Unsigned u8 = cur->dfe_index;
1031 WRITE_UNALIGNED(dbg, (void *) data,
1032 (const void *) &u8,
1033 sizeof(u8), DWARF_64BIT_SIZE);
1034 data += DWARF_64BIT_SIZE;
1035 }
1036 break;
1037 case DW_FORM_block:
1038 case DW_FORM_exprloc:
1039 default:
1040 DWARF_P_DBG_ERROR(dbg,
1041 DW_DLE_LNCT_FORM_CODE_NOT_HANDLED,
1042 DW_DLV_ERROR);
1043 }
1044 }
1045 break;
1046 case DW_LNCT_size: {
1047 switch(cform) {
1048 case DW_FORM_data1:
1049 calculated_size += 1;
1050 if (write_out) {
1051 unsigned char ub = cur->dfe_index;
1052 *data = ub;
1053 data += 1;
1054 }
1055 break;
1056 case DW_FORM_data2:
1057 calculated_size += DWARF_HALF_SIZE;
1058 if (write_out) {
1059 Dwarf_Half uh = cur->dfe_index;
1060 memcpy(data,&uh,DWARF_HALF_SIZE);
1061
1062 }
1063 break;
1064 case DW_FORM_data4:
1065 calculated_size += DWARF_32BIT_SIZE;
1066 if (write_out) {
1067 ASNOUT(data,cur->dfe_index,
1068 DWARF_32BIT_SIZE);
1069 data += DWARF_32BIT_SIZE;
1070 }
1071 break;
1072 case DW_FORM_data8:
1073 calculated_size += DWARF_64BIT_SIZE;
1074 if (write_out) {
1075 Dwarf_Unsigned u8 = cur->dfe_index;
1076 WRITE_UNALIGNED(dbg, (void *) data,
1077 (const void *) &u8,
1078 sizeof(u8), DWARF_64BIT_SIZE);
1079 data += DWARF_64BIT_SIZE;
1080 }
1081 break;
1082 case DW_FORM_udata: {
1083 unsigned val_len = 0;
1084 if (write_out) {
1085 res = append_uval(cur->dfe_size,
1086 dbg,
1087 data,
1088 &val_len,error);
1089 data += val_len;
1090 } else {
1091 res = pretend_write_uval(cur->dfe_size,
1092 dbg, &val_len,error);
1093 }
1094 if (res != DW_DLV_OK) {
1095 return res;
1096 }
1097 calculated_size += val_len;
1098 }
1099 break;
1100 default:
1101 DWARF_P_DBG_ERROR(dbg,
1102 DW_DLE_LNCT_FORM_CODE_NOT_HANDLED,
1103 DW_DLV_ERROR);
1104 }
1105 }
1106 break;
1107 case DW_LNCT_MD5: {
1108 switch(cform) {
1109 case DW_FORM_data16:
1110 if (write_out) {
1111 memcpy(data,cur->dfe_md5,
1112 sizeof(cur->dfe_md5));
1113 data += 16;
1114 }
1115 calculated_size += 16;
1116 break;
1117 default:
1118 DWARF_P_DBG_ERROR(dbg,
1119 DW_DLE_LNCT_FORM_CODE_NOT_HANDLED,
1120 DW_DLV_ERROR);
1121 }
1122 }
1123 break;
1124 default:
1125 DWARF_P_DBG_ERROR(dbg, DW_DLE_LNCT_CODE_UNKNOWN,
1126 DW_DLV_ERROR);
1127 }
1128 }
1129 }
1130 *size_out = calculated_size;
1131 return DW_DLV_OK;
1132 }
1133
1134 static int
1135 calculate_size_of_line_header5(Dwarf_P_Debug dbg,
1136 struct Dwarf_P_Line_Inits_s *inits,
1137 unsigned *prolog_size_out,
1138 Dwarf_Error *error)
1139 {
1140 unsigned prolog_size = 0;
1141 int offset_size = dbg->de_dwarf_offset_size;
1142 int extension_size = dbg->de_64bit_extension ? 4 : 0;
1143 int res = 0;
1144
1145 prolog_size += OFFSET_PLUS_EXTENSION_SIZE +
1146 sizeof_uhalf(dbg) + /* version # */
1147 sizeof_ubyte(dbg) + /* address_size */
1148 sizeof_ubyte(dbg) + /* segment_selector_size */
1149 offset_size + /* header length */
1150 sizeof_ubyte(dbg) + /* min_instr length */
1151 sizeof_ubyte(dbg) + /* maximum_operations_per_instruction */
1152 sizeof_ubyte(dbg) + /* default is_stmt */
1153 sizeof_ubyte(dbg) + /* linebase */
1154 sizeof_ubyte(dbg) + /* linerange */
1155 sizeof_ubyte(dbg); /* opcode base */
1156 /* For maximum_operations_per_instruction. */
1157 prolog_size += sizeof_ubyte(dbg);
1158
1159 /* standard_opcode_lengths table len */
1160 prolog_size += inits->pi_opcode_base-1;
1161
1162 {
1163 unsigned fsize = 0;
1164 res = determine_form_size(dbg,
1165 inits->pi_directory_entry_format_count,
1166 inits->pi_incformats,
1167 &fsize,FALSE,0,error);
1168 if (res != DW_DLV_OK) {
1169 return res;
1170 }
1171 prolog_size += fsize;
1172 }
1173 {
1174 unsigned dir_count_len = 0;
1175 res = determine_file_content_size(dbg,
1176 dbg->de_inc_dirs,
1177 dbg->de_line_inits.pi_directory_entry_format_count,
1178 dbg->de_line_inits.pi_incformats,
1179 &dir_count_len,
1180 FALSE,0,
1181 error);
1182 if (res != DW_DLV_OK) {
1183 return res;
1184 }
1185 prolog_size += dir_count_len;
1186 }
1187 {
1188 unsigned fsize = 0;
1189 res = determine_form_size(dbg,
1190 inits->pi_file_entry_format_count,
1191 inits->pi_fileformats,
1192 &fsize,
1193 FALSE,0,
1194 error);
1195 if (res != DW_DLV_OK) {
1196 return res;
1197 }
1198 prolog_size += fsize;
1199 }
1200 {
1201 unsigned file_count_len = 0;
1202 res = determine_file_content_size(dbg,
1203 dbg->de_file_entries,
1204 dbg->de_line_inits.pi_file_entry_format_count,
1205 dbg->de_line_inits.pi_fileformats,
1206 &file_count_len,
1207 FALSE,0,
1208 error);
1209 if (res != DW_DLV_OK) {
1210 return res;
1211 }
1212 prolog_size += file_count_len;
1213 }
1214 *prolog_size_out = prolog_size;
1215 return DW_DLV_OK;
1216 }
1217
1218 /* For DWARF 2,3,4 */
1219 static int
1220 calculate_size_of_line_header4(Dwarf_P_Debug dbg,
1221 struct Dwarf_P_Line_Inits_s *inits,
1222 unsigned *prolog_size_out,
1223 Dwarf_Error *error)
1224 {
1225 Dwarf_P_F_Entry curdir = 0;
1226 Dwarf_P_F_Entry curentry = 0;
1227 unsigned prolog_size = 0;
1228 int offset_size = dbg->de_dwarf_offset_size;
1229 int extension_size = dbg->de_64bit_extension ? 4 : 0;
1230
1231 (void)error;
1232 prolog_size += OFFSET_PLUS_EXTENSION_SIZE +
1233 sizeof_uhalf(dbg) + /* version # */
1234 offset_size + /* header length */
1235 sizeof_ubyte(dbg) + /* min_instr length */
1236 sizeof_ubyte(dbg) + /* default is_stmt */
1237 sizeof_ubyte(dbg) + /* linebase */
1238 sizeof_ubyte(dbg) + /* linerange */
1239 sizeof_ubyte(dbg); /* opcode base */
1240 if (inits->pi_linetable_version == DW_LINE_VERSION4) {
1241 /* For maximum_operations_per_instruction. */
1242 prolog_size += sizeof_ubyte(dbg);
1243 }
1244 /* standard_opcode_lengths table len */
1245 prolog_size += inits->pi_opcode_base-1;
1246
1247 /* include directories */
1248 curdir = dbg->de_inc_dirs;
1249 while (curdir) {
1250 prolog_size += strlen(curdir->dfe_name) + 1;
1251 curdir = curdir->dfe_next;
1252 }
1253 prolog_size++; /* last null following last directory
1254 entry. */
1255
1256 /* file entries */
1257 curentry = dbg->de_file_entries;
1258 while (curentry) {
1259 prolog_size +=
1260 strlen(curentry->dfe_name) + 1 + curentry->dfe_nbytes;
1261 curentry = curentry->dfe_next;
1262 }
1263 prolog_size++; /* last null byte */
1264 *prolog_size_out = prolog_size;
1265 return DW_DLV_OK;
1266 }
1267
1268 /* Generate debug_line section
1269 Dwarf2, dwarf3 headers are the same (DW3 acknowledges 64bit).
1270 DWARF4 adds the maximum_operations_per_instruction field.
1271 DWARF5 adds address size and address selector size
1272 and replaces the entire directories/files list with
1273 very different stuff.
1274 */
1275 static int
1276 _dwarf_pro_generate_debugline(Dwarf_P_Debug dbg,
1277 Dwarf_Unsigned * nbufs,
1278 Dwarf_Error * error)
1279 {
1280 Dwarf_P_F_Entry curdir = 0;
1281 Dwarf_P_F_Entry curentry = 0;
1282 Dwarf_P_Line curline = 0;
1283 Dwarf_P_Line prevline = 0;
1284 struct Dwarf_P_Line_Inits_s *inits = 0;
1285
1286 /* all data named cur* are used to loop thru linked lists */
1287
1288 int sum_bytes = 0;
1289 unsigned prolog_size = 0;
1290 unsigned char *data = 0; /* holds disk form data */
1291 int elfsectno = 0;
1292 unsigned char *start_line_sec = 0; /* pointer to the buffer at
1293 section start */
1294 /* temps for memcpy */
1295 Dwarf_Unsigned du = 0;
1296 Dwarf_Ubyte db = 0;
1297 Dwarf_Half dh = 0;
1298 int res = 0;
1299 Dwarf_Half version = dbg->de_output_version;
1300 int offset_size = dbg->de_dwarf_offset_size;
1301 Dwarf_Ubyte extension_size = dbg->de_64bit_extension ? 4 : 0;
1302 Dwarf_Ubyte address_size = dbg->de_pointer_size;
1303
1304 sum_bytes = 0;
1305
1306 elfsectno = dbg->de_elf_sects[DEBUG_LINE];
1307
1308 inits = &dbg->de_line_inits;
1309 if (version < 5) {
1310 res = calculate_size_of_line_header4(dbg,inits,&prolog_size,
1311 error);
1312 } else if (version == 5) {
1313 res = calculate_size_of_line_header5(dbg,inits,&prolog_size,
1314 error);
1315 } else {
1316 _dwarf_p_error(dbg, error,DW_DLE_VERSION_STAMP_ERROR );
1317 return DW_DLV_ERROR;
1318 }
1319 if (res != DW_DLV_OK) {
1320 return res;
1321 }
1322 /* Allocate a chunk, put address in 'data' */
1323 GET_CHUNK_ERR(dbg, elfsectno, data, prolog_size, error);
1324
1325 start_line_sec = data;
1326
1327 /* Copy the prologue data into 'data' */
1328 /* total_length */
1329 du = 0;
1330 if (extension_size) {
1331 DISTINGUISHED_VALUE_ARRAY(v4);
1332
1333 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &v4[0],
1334 SIZEOFT32, extension_size);
1335 data += extension_size;
1336 }
1337
1338 /* We will adjust this later, we do not know the full length
1339 of the line_section content for this cu yet. */
1340 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &du,
1341 sizeof(du), offset_size);
1342 data += offset_size;
1343
1344 dh = inits->pi_linetable_version;
1345 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &dh,
1346 sizeof(dh), DWARF_HALF_SIZE);
1347 data += DWARF_HALF_SIZE;
1348 if (version == 5 ) {
1349 /* address size, seg sel size now */
1350 db = inits->pi_address_size;
1351 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
1352 sizeof(db), sizeof(db));
1353 data += sizeof(db);
1354 db = inits->pi_segment_size; /* segment selector size */
1355 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
1356 sizeof(db), sizeof(db));
1357 data += sizeof(db);
1358 }
1359
1360 {
1361 /* header length (called prolog length in DWARF2)
1362 This we do know, we calculated the prolog length
1363 already and it is prolog_size so just
1364 */
1365 Dwarf_Unsigned sofar = data - start_line_sec;
1366
1367 du = prolog_size - sofar - offset_size;
1368 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &du,
1369 sizeof(du), offset_size);
1370 data += offset_size;
1371 }
1372 db = inits->pi_minimum_instruction_length;
1373 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
1374 sizeof(db), sizeof(Dwarf_Ubyte));
1375 data += sizeof(Dwarf_Ubyte);
1376
1377 if (inits->pi_linetable_version == 4 ||
1378 inits->pi_linetable_version == 5) {
1379 db = inits->pi_maximum_operations_per_instruction;
1380 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
1381 sizeof(db), sizeof(Dwarf_Ubyte));
1382 data += sizeof(Dwarf_Ubyte);
1383 }
1384
1385 db = inits->pi_default_is_stmt;
1386 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
1387 sizeof(db), sizeof(Dwarf_Ubyte));
1388 data += sizeof(Dwarf_Ubyte);
1389 db = inits->pi_line_base;
1390 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
1391 sizeof(db), sizeof(Dwarf_Ubyte));
1392 data += sizeof(Dwarf_Ubyte);
1393 db = inits->pi_line_range;
1394 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
1395 sizeof(db), sizeof(Dwarf_Ubyte));
1396 data += sizeof(Dwarf_Ubyte);
1397 db = inits->pi_opcode_base;
1398 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
1399 sizeof(db), sizeof(Dwarf_Ubyte));
1400 data += sizeof(Dwarf_Ubyte);
1401 /* stream of single bytes. No endian issues. */
1402 memcpy((void *) data, (const void *) std_opcode_len,
1403 inits->pi_opcode_base-1);
1404 data += inits->pi_opcode_base-1;
1405
1406 if (version < 5) {
1407 /* copy over include directories */
1408 curdir = dbg->de_inc_dirs;
1409 while (curdir) {
1410 strcpy((char *) data, curdir->dfe_name);
1411 data += strlen(curdir->dfe_name) + 1;
1412 curdir = curdir->dfe_next;
1413 }
1414 *data = '\0'; /* last null */
1415 data++;
1416
1417 /* copy file entries */
1418 curentry = dbg->de_file_entries;
1419 while (curentry) {
1420 strcpy((char *) data, curentry->dfe_name);
1421 data += strlen(curentry->dfe_name) + 1;
1422 /* copies of leb numbers, no endian issues */
1423 memcpy((void *) data,
1424 (const void *) curentry->dfe_args,
1425 curentry->dfe_nbytes);
1426 data += curentry->dfe_nbytes;
1427 curentry = curentry->dfe_next;
1428 }
1429 *data = '\0';
1430 data++;
1431 } else if (version == 5) {
1432 {
1433 unsigned fsize = 0;
1434 res = determine_form_size(dbg,
1435 inits->pi_directory_entry_format_count,
1436 inits->pi_incformats,
1437 &fsize,
1438 TRUE,data,
1439 error);
1440 if (res != DW_DLV_OK) {
1441 return res;
1442 }
1443 data += fsize;
1444 }
1445 {
1446 unsigned dir_count_len = 0;
1447 res = determine_file_content_size(dbg,
1448 dbg->de_inc_dirs,
1449 inits->pi_directory_entry_format_count,
1450 inits->pi_incformats,
1451 &dir_count_len,
1452 TRUE,data,
1453 error);
1454 if (res != DW_DLV_OK) {
1455 return res;
1456 }
1457 data += dir_count_len;
1458 }
1459 {
1460 unsigned fsize = 0;
1461 res = determine_form_size(dbg,
1462 inits->pi_file_entry_format_count,
1463 inits->pi_fileformats,
1464 &fsize,
1465 TRUE,data,
1466 error);
1467 if (res != DW_DLV_OK) {
1468 return res;
1469 }
1470 data += fsize;
1471 }
1472 {
1473 unsigned file_count_len = 0;
1474 res = determine_file_content_size(dbg,
1475 dbg->de_file_entries,
1476 dbg->de_line_inits.pi_file_entry_format_count,
1477 dbg->de_line_inits.pi_fileformats,
1478 &file_count_len,
1479 TRUE,data,
1480 error);
1481 if (res != DW_DLV_OK) {
1482 return res;
1483 }
1484 data += file_count_len;
1485 }
1486 }
1487
1488 {
1489 Dwarf_Unsigned sofar = data - start_line_sec;
1490 if (sofar != prolog_size) {
1491 /* We miscalculated something. */
1492 _dwarf_p_error(dbg, error,
1493 DW_DLE_LINE_HEADER_LENGTH_BOTCH);
1494 return DW_DLV_ERROR;
1495 }
1496 sum_bytes += prolog_size;
1497 }
1498
1499 curline = dbg->de_lines;
1500 prevline = (Dwarf_P_Line)
1501 _dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_P_Line_s));
1502 if (prevline == NULL) {
1503 DWARF_P_DBG_ERROR(dbg, DW_DLE_LINE_ALLOC, DW_DLV_ERROR);
1504 }
1505 _dwarf_pro_reg_init(dbg,prevline);
1506 /* generate opcodes for line numbers */
1507 while (curline) {
1508 int opc = 0;
1509 int no_lns_copy = 0; /* if lns copy opcode does not need to be
1510 generated, if special opcode or end
1511 sequence */
1512 Dwarf_Unsigned addr_adv = 0;
1513 int line_adv = 0; /* supposed to be a reasonably small
1514 number, so the size should not be a
1515 problem. ? */
1516
1517 no_lns_copy = 0;
1518 if (curline->dpl_opc != 0) {
1519 int inst_bytes = 0; /* no of bytes in extended opcode */
1520 unsigned writelen = 0;
1521
1522 switch (curline->dpl_opc) {
1523 case DW_LNE_end_sequence:
1524 /* Advance pc to end of text section. */
1525 addr_adv = curline->dpl_address -
1526 prevline->dpl_address;
1527 if (addr_adv > 0) {
1528 res = write_opcode_uval(DW_LNS_advance_pc,dbg,
1529 elfsectno,
1530 addr_adv/inits->pi_minimum_instruction_length,
1531 &writelen,
1532 error);
1533 if (res != DW_DLV_OK) {
1534 return res;
1535 }
1536 sum_bytes += writelen;
1537 prevline->dpl_address = curline->dpl_address;
1538 }
1539
1540 /* first null byte */
1541 db = 0;
1542 res = write_ubyte(db,dbg,elfsectno,
1543 &writelen,error);
1544 if (res != DW_DLV_OK) {
1545 return res;
1546 }
1547 sum_bytes += writelen;
1548
1549 /* write length of extended opcode */
1550 inst_bytes = sizeof(Dwarf_Ubyte);
1551 res = write_uval(inst_bytes,dbg,elfsectno,
1552 &writelen,error);
1553 if (res != DW_DLV_OK) {
1554 return res;
1555 }
1556 sum_bytes += writelen;
1557
1558 /* write extended opcode */
1559 res = write_ubyte(DW_LNE_end_sequence,dbg,elfsectno,
1560 &writelen,error);
1561 if (res != DW_DLV_OK) {
1562 return res;
1563 }
1564 sum_bytes += writelen;
1565
1566 /* reset value to original values */
1567 _dwarf_pro_reg_init(dbg,prevline);
1568 no_lns_copy = 1;
1569 /* this is set only for end_sequence, so that a
1570 dw_lns_copy is not generated */
1571 break;
1572
1573 case DW_LNE_set_address:
1574
1575 /* first null byte */
1576 db = 0;
1577 res = write_ubyte(db,dbg,elfsectno,
1578 &writelen,error);
1579 if (res != DW_DLV_OK) {
1580 return res;
1581 }
1582 sum_bytes += writelen;
1583
1584 /* write length of extended opcode */
1585 inst_bytes = sizeof(Dwarf_Ubyte) + address_size;
1586 res = write_uval(inst_bytes,dbg,elfsectno,
1587 &writelen,error);
1588 if (res != DW_DLV_OK) {
1589 return res;
1590 }
1591 sum_bytes += writelen;
1592
1593 /* write extended opcode */
1594 res = write_ubyte(DW_LNE_set_address,dbg,elfsectno,
1595 &writelen,error);
1596 if (res != DW_DLV_OK) {
1597 return res;
1598 }
1599 sum_bytes += writelen;
1600
1601 /* reloc for address */
1602 res = dbg->de_relocate_by_name_symbol(dbg,
1603 DEBUG_LINE,
1604 sum_bytes, /* r_offset */
1605 curline->dpl_r_symidx,
1606 dwarf_drt_data_reloc,
1607 offset_size);
1608 if (res != DW_DLV_OK) {
1609 DWARF_P_DBG_ERROR(dbg, DW_DLE_CHUNK_ALLOC,
1610 DW_DLV_ERROR);
1611 }
1612
1613 /* write offset (address) */
1614 du = curline->dpl_address;
1615 res = write_fixed_size(du,dbg,elfsectno,
1616 address_size,&writelen,error);
1617 if (res != DW_DLV_OK) {
1618 return res;
1619 }
1620 sum_bytes += writelen;
1621 prevline->dpl_address = curline->dpl_address;
1622 no_lns_copy = 1;
1623 break;
1624 case DW_LNE_define_file:
1625 /* Not supported, all add-file entries
1626 are added via dbg -> de_file_entries,
1627 which adds to the line table header. */
1628 no_lns_copy = 1;
1629 break;
1630 case DW_LNE_set_discriminator: {/* DWARF4 */
1631 unsigned val_len = 0;
1632 /* first null byte */
1633 db = 0;
1634 res = write_ubyte(db,dbg,elfsectno,&writelen,error);
1635 if (res != DW_DLV_OK) {
1636 return res;
1637 }
1638 sum_bytes += writelen;
1639
1640 /* Write len of opcode + value here. */
1641 res = pretend_write_uval(curline->dpl_discriminator,
1642 dbg, &val_len,error);
1643 if (res != DW_DLV_OK) {
1644 return res;
1645 }
1646 val_len++;
1647
1648 res = write_uval(val_len +1,dbg,elfsectno,
1649 &writelen,error);
1650 if (res != DW_DLV_OK) {
1651 return res;
1652 }
1653 sum_bytes += writelen;
1654
1655 /* Write opcode */
1656 res = write_ubyte(DW_LNE_set_discriminator,
1657 dbg,elfsectno,
1658 &writelen,error);
1659 if (res != DW_DLV_OK) {
1660 return res;
1661 }
1662 sum_bytes += writelen;
1663
1664 /* Write the value itself. */
1665 res = write_uval(curline->dpl_discriminator,
1666 dbg,elfsectno,&writelen,error);
1667 if (res != DW_DLV_OK) {
1668 return res;
1669 }
1670 sum_bytes += writelen;
1671 no_lns_copy = 1;
1672 }
1673 break;
1674 }
1675 } else {
1676 unsigned writelen = 0;
1677 if (inits->pi_opcode_base >12) {
1678 /* We have the newer standard opcodes
1679 DW_LNS_set_prologue_end, DW_LNS_set_epilogue_end,
1680 DW_LNS_set_isa, we do not write them if not
1681 in the table. DWARF3 and DWARF4 */
1682 /* Should we check if a change?
1683 These reset automatically
1684 in the line processing/reading engine,
1685 so I think no check of prevline is wanted. */
1686 if (curline->dpl_epilogue_begin) {
1687 res = write_ubyte(DW_LNS_set_epilogue_begin,dbg,
1688 elfsectno,&writelen, error);
1689 if (res != DW_DLV_OK) {
1690 return res;
1691 }
1692 sum_bytes += writelen;
1693 }
1694 if (curline->dpl_prologue_end) {
1695 res = write_ubyte(DW_LNS_set_prologue_end,dbg,
1696 elfsectno, &writelen,error);
1697 if (res != DW_DLV_OK) {
1698 return res;
1699 }
1700 sum_bytes += writelen;
1701 }
1702 if (curline->dpl_isa != prevline->dpl_isa) {
1703 res = write_opcode_uval(DW_LNS_set_isa,dbg,
1704 elfsectno, curline->dpl_isa,
1705 &writelen ,error);
1706 if (res != DW_DLV_OK) {
1707 return res;
1708 }
1709 sum_bytes += writelen;
1710 }
1711 }
1712 if (curline->dpl_file != prevline->dpl_file) {
1713 db = DW_LNS_set_file;
1714 res = write_opcode_uval(db,dbg,
1715 elfsectno,
1716 curline->dpl_file,&writelen ,error);
1717 if (res != DW_DLV_OK) {
1718 return res;
1719 }
1720 sum_bytes += writelen;
1721
1722 prevline->dpl_file = curline->dpl_file;
1723 }
1724 if (curline->dpl_column != prevline->dpl_column) {
1725 db = DW_LNS_set_column;
1726 res = write_opcode_uval(db,dbg,
1727 elfsectno, curline->dpl_column , &writelen,error);
1728 if (res != DW_DLV_OK) {
1729 return res;
1730 }
1731 sum_bytes += writelen;
1732 prevline->dpl_column = curline->dpl_column;
1733 }
1734 if (curline->dpl_is_stmt != prevline->dpl_is_stmt) {
1735 res = write_ubyte(DW_LNS_negate_stmt,dbg,elfsectno,
1736 &writelen,error);
1737 if (res != DW_DLV_OK) {
1738 return res;
1739 }
1740 sum_bytes += writelen;
1741 prevline->dpl_is_stmt = curline->dpl_is_stmt;
1742 }
1743 if (curline->dpl_basic_block == TRUE &&
1744 prevline->dpl_basic_block == FALSE) {
1745 res = write_ubyte(DW_LNS_set_basic_block,dbg,
1746 elfsectno,&writelen,error);
1747 if (res != DW_DLV_OK) {
1748 return res;
1749 }
1750 sum_bytes += writelen;
1751 prevline->dpl_basic_block = curline->dpl_basic_block;
1752 }
1753 if (curline->dpl_discriminator) {
1754 /* This is dwarf4, but because it is an extended op
1755 not a standard op,
1756 we allow it without testing version.
1757 GNU seems to set this from time to time. */
1758 unsigned val_len = 0;
1759 /* first null byte */
1760 db = 0;
1761 res = write_ubyte(db,dbg,elfsectno,&writelen,error);
1762 if (res != DW_DLV_OK) {
1763 return res;
1764 }
1765 sum_bytes += writelen;
1766
1767 /* Write len of opcode + value here. */
1768 res = pretend_write_uval(curline->dpl_discriminator,
1769 dbg, &val_len,error);
1770 if (res != DW_DLV_OK) {
1771 return res;
1772 }
1773 val_len ++;
1774 res = write_uval(val_len +1,dbg,elfsectno,
1775 &writelen,error);
1776 if (res != DW_DLV_OK) {
1777 return res;
1778 }
1779 sum_bytes += writelen;
1780
1781 /* Write opcode */
1782 res = write_ubyte(DW_LNE_set_discriminator,
1783 dbg,elfsectno,&writelen,error);
1784 if (res != DW_DLV_OK) {
1785 return res;
1786 }
1787 sum_bytes += writelen;
1788
1789 /* Write the value itself. */
1790 res = write_uval(curline->dpl_discriminator,
1791 dbg,elfsectno,&writelen,error);
1792 if (res != DW_DLV_OK) {
1793 return res;
1794 }
1795 sum_bytes += writelen;
1796 }
1797
1798 addr_adv = curline->dpl_address - prevline->dpl_address;
1799
1800 line_adv = (int) (curline->dpl_line - prevline->dpl_line);
1801 if ((addr_adv % MIN_INST_LENGTH) != 0) {
1802 DWARF_P_DBG_ERROR(dbg, DW_DLE_WRONG_ADDRESS,
1803 DW_DLV_ERROR);
1804 }
1805 opc = _dwarf_pro_get_opc(inits,addr_adv, line_adv);
1806 if (opc > 0) {
1807 /* Use special opcode. */
1808 no_lns_copy = 1;
1809 res = write_ubyte(opc,dbg,elfsectno,&writelen,error);
1810 if (res != DW_DLV_OK) {
1811 return res;
1812 }
1813 sum_bytes += writelen;
1814 prevline->dpl_basic_block = FALSE;
1815 prevline->dpl_address = curline->dpl_address;
1816 prevline->dpl_line = curline->dpl_line;
1817 } else {
1818 /* opc says use standard opcodes. */
1819 if (addr_adv > 0) {
1820 db = DW_LNS_advance_pc;
1821 res = write_opcode_uval(db,dbg,
1822 elfsectno,
1823 addr_adv/inits->pi_minimum_instruction_length,
1824 &writelen,
1825 error);
1826 if (res != DW_DLV_OK) {
1827 return res;
1828 }
1829 sum_bytes += writelen;
1830 prevline->dpl_basic_block = FALSE;
1831 prevline->dpl_address = curline->dpl_address;
1832 }
1833 if (line_adv != 0) {
1834 db = DW_LNS_advance_line;
1835 res = write_ubyte(db,dbg,
1836 elfsectno,
1837 &writelen,
1838 error);
1839 if (res != DW_DLV_OK) {
1840 return res;
1841 }
1842 sum_bytes += writelen;
1843 res = write_sval(line_adv,dbg,
1844 elfsectno,
1845 &writelen,
1846 error);
1847 if (res != DW_DLV_OK) {
1848 return res;
1849 }
1850 sum_bytes += writelen;
1851 prevline->dpl_basic_block = FALSE;
1852 prevline->dpl_line = curline->dpl_line;
1853 }
1854 }
1855 } /* ends else for opc <= 0 */
1856 if (no_lns_copy == 0) { /* if not a special or dw_lne_end_seq
1857 generate a matrix line */
1858 unsigned writelen = 0;
1859 res = write_ubyte(DW_LNS_copy,dbg,elfsectno,
1860 &writelen,error);
1861 if (res != DW_DLV_OK) {
1862 return res;
1863 }
1864 sum_bytes += writelen;
1865 prevline->dpl_basic_block = FALSE;
1866 }
1867 curline = curline->dpl_next;
1868 }
1869
1870 /* write total length field */
1871 du = sum_bytes - OFFSET_PLUS_EXTENSION_SIZE;
1872 {
1873 start_line_sec += extension_size;
1874 WRITE_UNALIGNED(dbg, (void *) start_line_sec,
1875 (const void *) &du, sizeof(du), offset_size);
1876 }
1877
1878 *nbufs = dbg->de_n_debug_sect;
1879 return DW_DLV_OK;
1880 }
1881
1882 /*
1883 Generate debug_frame section */
1884 static int
1885 _dwarf_pro_generate_debugframe(Dwarf_P_Debug dbg,
1886 Dwarf_Unsigned *nbufs,
1887 Dwarf_Error * error)
1888 {
1889 int elfsectno = 0;
1890 int i = 0;
1891 int firsttime = 1;
1892 Dwarf_P_Cie curcie = 0;
1893 Dwarf_P_Fde curfde = 0;
1894 unsigned char *data = 0;
1895 Dwarf_Unsigned du = 0;
1896 Dwarf_Ubyte db = 0;
1897 long *cie_offs = 0; /* Holds byte offsets for links to fde's */
1898 unsigned long cie_length = 0;
1899 int cie_no = 0;
1900 Dwarf_Ubyte offset_size = dbg->de_dwarf_offset_size;
1901 Dwarf_Ubyte extension_size = dbg->de_64bit_extension ? 4 : 0;
1902 Dwarf_Ubyte address_size = dbg->de_pointer_size;
1903 Dwarf_Unsigned cur_off = 0;/* current offset of written data, held
1904 for relocation info */
1905
1906 elfsectno = dbg->de_elf_sects[DEBUG_FRAME];
1907
1908 curcie = dbg->de_frame_cies;
1909 cie_length = 0;
1910 cie_offs = (long *)
1911 _dwarf_p_get_alloc(dbg, sizeof(long) * dbg->de_n_cie);
1912 if (cie_offs == NULL) {
1913 DWARF_P_DBG_ERROR(dbg, DW_DLE_CIE_OFFS_ALLOC, DW_DLV_ERROR);
1914 }
1915 /* Generate cie number as we go along. This writes
1916 all CIEs first before any FDEs, which is rather
1917 different from the order a compiler might like (which
1918 might be each CIE followed by its FDEs then the next CIE, and
1919 so on). */
1920 cie_no = 1;
1921 while (curcie) {
1922 char *code_al = 0;
1923 int codeal_bytes = 0;
1924 char *data_al = 0;
1925 int data_align_bytes = 0;
1926 int pad = 0; /* Pad for padding to align cies and fdes */
1927 int res = 0;
1928 char buff1[ENCODE_SPACE_NEEDED];
1929 char buff2[ENCODE_SPACE_NEEDED];
1930 char buff3[ENCODE_SPACE_NEEDED];
1931 char *augmentation = 0;
1932 char *augmented_al = 0;
1933 long augmented_fields_length = 0;
1934 int irix_auglen_v0 = 0;
1935 Dwarf_Half version = curcie->cie_version;
1936
1937 res = dwarf_encode_leb128(curcie->cie_code_align,
1938 &codeal_bytes,
1939 buff1, sizeof(buff1));
1940 if (res != DW_DLV_OK) {
1941 DWARF_P_DBG_ERROR(dbg, DW_DLE_CIE_OFFS_ALLOC,
1942 DW_DLV_ERROR);
1943 }
1944 /* Before April 1999, the following was using an unsigned
1945 encode. That worked ok even though the decoder used the
1946 correct signed leb read, but doing the encode correctly
1947 (according to the dwarf spec)
1948 saves space in the output file
1949 and is completely compatible.
1950
1951 Note the actual stored amount on MIPS was 10 bytes (!) to
1952 store the value -4. (hex)fc ffffffff ffffffff 01 The
1953 libdwarf consumer consumed all 10 bytes too!
1954
1955 old version res =
1956 dwarf_encode_leb128(curcie->cie_data_align,
1957
1958 below is corrected signed version. */
1959 res = dwarf_encode_signed_leb128(
1960 curcie->cie_data_align,
1961 &data_align_bytes,
1962 buff2, sizeof(buff2));
1963 if (res != DW_DLV_OK) {
1964 DWARF_P_DBG_ERROR(dbg, DW_DLE_CIE_OFFS_ALLOC,
1965 DW_DLV_ERROR);
1966 }
1967 code_al = buff1;
1968 data_al = buff2;
1969
1970 /* get the correct offset */
1971 if (firsttime) {
1972 cie_offs[cie_no - 1] = 0;
1973 firsttime = 0;
1974 } else {
1975 cie_offs[cie_no - 1] = cie_offs[cie_no - 2] +
1976 (long) cie_length + OFFSET_PLUS_EXTENSION_SIZE;
1977 }
1978 cie_no++;
1979 augmentation = curcie->cie_aug;
1980 cie_length = offset_size + /* cie_id */
1981 sizeof(Dwarf_Ubyte) + /* cie version */
1982 strlen(curcie->cie_aug) + 1 + /* augmentation */
1983 codeal_bytes + /* code alignment factor */
1984 data_align_bytes + /* data alignment factor */
1985 sizeof(Dwarf_Ubyte) + /* return reg address */
1986 curcie->cie_inst_bytes;
1987 if (dbg->de_irix_exc_augmentation &&
1988 (strcmp(augmentation, DW_CIE_AUGMENTER_STRING_V0) == 0)) {
1989
1990 /* IRIX specific. */
1991 augmented_fields_length = 0;
1992 res = dwarf_encode_leb128(augmented_fields_length,
1993 &irix_auglen_v0, buff3,
1994 sizeof(buff3));
1995 augmented_al = buff3;
1996 if (res != DW_DLV_OK) {
1997 DWARF_P_DBG_ERROR(dbg, DW_DLE_CIE_OFFS_ALLOC,
1998 DW_DLV_ERROR);
1999 }
2000 cie_length += irix_auglen_v0 ; /* augmentation length */
2001 }
2002 if (version >= 4) {
2003 /* address size, segment selector size */
2004 cie_length += 1 +1;
2005 }
2006
2007 pad = (int) PADDING(cie_length, address_size);
2008 cie_length += pad;
2009
2010 /* Now we have the cie length with padding,
2011 allocate a buffer for that plus the header
2012 length. */
2013 GET_CHUNK_ERR(dbg, elfsectno, data, cie_length +
2014 OFFSET_PLUS_EXTENSION_SIZE,
2015 error);
2016 if (extension_size) {
2017 DISTINGUISHED_VALUE_ARRAY(v4);
2018
2019 WRITE_UNALIGNED(dbg, (void *) data,
2020 (const void *) &v4[0],
2021 SIZEOFT32, extension_size);
2022 data += extension_size;
2023
2024 }
2025 du = cie_length;
2026 /* total length of cie */
2027 WRITE_UNALIGNED(dbg, (void *) data,
2028 (const void *) &du, sizeof(du), offset_size);
2029 data += offset_size;
2030
2031 /* cie-id is a special value. */
2032 du = DW_CIE_ID;
2033 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &du,
2034 sizeof(du), offset_size);
2035 data += offset_size;
2036
2037 db = curcie->cie_version;
2038 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
2039 sizeof(db), sizeof(Dwarf_Ubyte));
2040 data += sizeof(Dwarf_Ubyte);
2041
2042 strcpy((char *) data, curcie->cie_aug);
2043 data += strlen(curcie->cie_aug) + 1;
2044
2045 if (curcie->cie_version >= 4) {
2046 /* emit address-size, segment selector size */
2047 db = dbg->de_pointer_size;
2048 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
2049 sizeof(db), sizeof(Dwarf_Ubyte));
2050 data += sizeof(Dwarf_Ubyte);
2051
2052 db = dbg->de_segment_selector_size;
2053 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
2054 sizeof(db), sizeof(Dwarf_Ubyte));
2055 data += sizeof(Dwarf_Ubyte);
2056 }
2057
2058 memcpy((void *) data, (const void *) code_al, codeal_bytes);
2059 data += codeal_bytes;
2060
2061 memcpy((void *) data, (const void *) data_al,
2062 data_align_bytes);
2063 data += data_align_bytes;
2064
2065 db = curcie->cie_ret_reg;
2066 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &db,
2067 sizeof(db), sizeof(Dwarf_Ubyte));
2068 data += sizeof(Dwarf_Ubyte);
2069
2070 if (dbg->de_irix_exc_augmentation &&
2071 strcmp(augmentation, DW_CIE_AUGMENTER_STRING_V0) == 0) {
2072
2073 /* IRIX only */
2074 memcpy((void *) data, (const void *) augmented_al,
2075 irix_auglen_v0);
2076 data += irix_auglen_v0;
2077 }
2078 memcpy((void *) data, (const void *) curcie->cie_inst,
2079 curcie->cie_inst_bytes);
2080 data += curcie->cie_inst_bytes;
2081
2082 for (i = 0; i < pad; i++) {
2083 *data = DW_CFA_nop;
2084 data++;
2085 }
2086 curcie = curcie->cie_next;
2087 }
2088 /* calculate current offset */
2089 cur_off = cie_offs[cie_no - 2] + cie_length +
2090 OFFSET_PLUS_EXTENSION_SIZE;
2091
2092 /* write out fde's */
2093 curfde = dbg->de_frame_fdes;
2094 while (curfde) {
2095 Dwarf_P_Frame_Pgm curinst = 0;
2096 long fde_length = 0;
2097 int pad2 = 0;
2098 Dwarf_P_Cie cie_ptr = 0;
2099 Dwarf_Unsigned cie_index = 0;
2100 /* index is a global in string.h, so don't
2101 name anything index. */
2102 Dwarf_Unsigned indx = 0;
2103 int oet_length = 0;
2104 int afl_length = 0;
2105 int res = 0;
2106 int v0_augmentation = 0;
2107 char afl_buff[ENCODE_SPACE_NEEDED];
2108
2109 /* Find the CIE associated with this fde. */
2110 cie_ptr = dbg->de_frame_cies;
2111 cie_index = curfde->fde_cie;
2112 indx = 1; /* The cie_index of the first cie is 1, not 0. */
2113 while (cie_ptr && indx < cie_index) {
2114 cie_ptr = cie_ptr->cie_next;
2115 indx++;
2116 }
2117 if (cie_ptr == NULL) {
2118 DWARF_P_DBG_ERROR(dbg, DW_DLE_CIE_NULL, DW_DLV_ERROR);
2119 }
2120
2121 fde_length = curfde->fde_n_bytes +
2122 OFFSET_PLUS_EXTENSION_SIZE +
2123 /* cie pointer */
2124 address_size + /* initial loc */
2125 address_size; /* address range */
2126 if (dbg->de_irix_exc_augmentation &&
2127 strcmp(cie_ptr->cie_aug,
2128 DW_CIE_AUGMENTER_STRING_V0) == 0) {
2129
2130 v0_augmentation = 1;
2131 oet_length = DWARF_32BIT_SIZE;
2132 /* encode the length of augmented fields. */
2133 res = dwarf_encode_leb128(oet_length,
2134 &afl_length, afl_buff,
2135 sizeof(afl_buff));
2136 if (res != DW_DLV_OK) {
2137 DWARF_P_DBG_ERROR(dbg, DW_DLE_CIE_OFFS_ALLOC,
2138 DW_DLV_ERROR);
2139 }
2140
2141 fde_length +=
2142 afl_length + /* augmented field length */
2143 oet_length; /* exception_table offset */
2144 }
2145
2146 if (curfde->fde_die) {
2147 /* IRIX/MIPS extension:
2148 Using fde offset, generate DW_AT_MIPS_fde
2149 attribute for the
2150 die corresponding to this fde. */
2151 res = _dwarf_pro_add_AT_fde(dbg, curfde->fde_die, cur_off,
2152 error);
2153 if (res != DW_DLV_OK) {
2154 return res;
2155 }
2156 }
2157
2158 /* store relocation for cie pointer */
2159
2160 res = dbg->de_relocate_by_name_symbol(dbg,
2161 DEBUG_FRAME, cur_off +
2162 OFFSET_PLUS_EXTENSION_SIZE /* r_offset */,
2163 dbg->de_sect_name_idx[DEBUG_FRAME],
2164 dwarf_drt_data_reloc, offset_size);
2165 if (res != DW_DLV_OK) {
2166 DWARF_P_DBG_ERROR(dbg, DW_DLE_CHUNK_ALLOC, res );
2167 }
2168
2169 /* store relocation information for initial location */
2170 res = dbg->de_relocate_by_name_symbol(dbg,
2171 DEBUG_FRAME,
2172 cur_off + OFFSET_PLUS_EXTENSION_SIZE +
2173 address_size /* r_offset */,
2174 curfde->fde_r_symidx,
2175 dwarf_drt_data_reloc, address_size);
2176 if (res != DW_DLV_OK) {
2177 DWARF_P_DBG_ERROR(dbg, DW_DLE_CHUNK_ALLOC, res);
2178 }
2179 /* Store the relocation information for the
2180 offset_into_exception_info field, if the
2181 offset is valid (0
2182 is a valid offset). */
2183 if (v0_augmentation &&
2184 curfde->fde_offset_into_exception_tables >= 0) {
2185
2186 res = dbg->de_relocate_by_name_symbol(dbg,
2187 DEBUG_FRAME,
2188 /* r_offset, where in cie this field starts */
2189 cur_off + OFFSET_PLUS_EXTENSION_SIZE +
2190 offset_size + 2 * address_size +
2191 afl_length,
2192 curfde->fde_exception_table_symbol,
2193 dwarf_drt_segment_rel,
2194 DWARF_32BIT_SIZE);
2195 if (res != DW_DLV_OK) {
2196 DWARF_P_DBG_ERROR(dbg, DW_DLE_CHUNK_ALLOC, res);
2197 }
2198 }
2199
2200 /* adjust for padding */
2201 pad2 = (int) PADDING(fde_length, address_size);
2202 fde_length += pad2;
2203
2204 /* write out fde */
2205 GET_CHUNK(dbg, elfsectno, data, fde_length +
2206 OFFSET_PLUS_EXTENSION_SIZE,
2207 error);
2208 du = fde_length;
2209 {
2210 if (extension_size) {
2211 DISTINGUISHED_VALUE_ARRAY(v4);
2212
2213 WRITE_UNALIGNED(dbg, (void *) data,
2214 (const void *) &v4[0],
2215 SIZEOFT32, extension_size);
2216 data += extension_size;
2217 }
2218 /* length */
2219 WRITE_UNALIGNED(dbg, (void *) data,
2220 (const void *) &du,
2221 sizeof(du), offset_size);
2222 data += offset_size;
2223
2224 /* offset to cie */
2225 du = cie_offs[curfde->fde_cie - 1];
2226 WRITE_UNALIGNED(dbg, (void *) data,
2227 (const void *) &du,
2228 sizeof(du), offset_size);
2229 data += offset_size;
2230
2231 du = curfde->fde_initloc;
2232 WRITE_UNALIGNED(dbg, (void *) data,
2233 (const void *) &du,
2234 sizeof(du), address_size);
2235 data += address_size;
2236
2237 if (dbg->de_relocate_pair_by_symbol &&
2238 curfde->fde_end_symbol != 0 &&
2239 curfde->fde_addr_range == 0) {
2240 /* symbolic reloc, need reloc for length What if we
2241 really know the length? If so, should use
2242 the other part of 'if'. */
2243 Dwarf_Unsigned val = 0;
2244
2245 res = dbg->de_relocate_pair_by_symbol(dbg,
2246 DEBUG_FRAME,
2247 cur_off + 2 * offset_size + address_size,
2248 /* r_offset */ curfde->fde_r_symidx,
2249 curfde->fde_end_symbol,
2250 dwarf_drt_first_of_length_pair,
2251 address_size);
2252 if (res != DW_DLV_OK) {
2253 {
2254 _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
2255 return DW_DLV_ERROR;
2256 }
2257 }
2258
2259 /* arrange pre-calc so assem text can do .word end -
2260 begin + val (gets val from stream) */
2261 val = curfde->fde_end_symbol_offset -
2262 curfde->fde_initloc;
2263 WRITE_UNALIGNED(dbg, data,
2264 (const void *) &val,
2265 sizeof(val), address_size);
2266 data += address_size;
2267 } else {
2268 du = curfde->fde_addr_range;
2269 WRITE_UNALIGNED(dbg, (void *) data,
2270 (const void *) &du,
2271 sizeof(du), address_size);
2272 data += address_size;
2273 }
2274 }
2275
2276 if (v0_augmentation) {
2277 /* IRIX only. */
2278 /* write the encoded augmented field length. */
2279 Dwarf_Signed dsw = 0;
2280
2281 memcpy((void *) data, (const void *) afl_buff,
2282 afl_length);
2283 data += afl_length;
2284 /* write the offset_into_exception_tables field. */
2285 dsw = (Dwarf_Signed)
2286 curfde->fde_offset_into_exception_tables;
2287 WRITE_UNALIGNED(dbg, (void *) data,
2288 (const void *) &dsw,
2289 sizeof(dsw), DWARF_32BIT_SIZE);
2290 data += DWARF_32BIT_SIZE;
2291 }
2292
2293 curinst = curfde->fde_inst;
2294 if (curfde->fde_block) {
2295 unsigned long size = curfde->fde_inst_block_size;
2296 memcpy((void *) data, (const void *) curfde->fde_block,
2297 size);
2298 data += size;
2299 } else {
2300 while (curinst) {
2301 db = curinst->dfp_opcode;
2302 WRITE_UNALIGNED(dbg, (void *) data,
2303 (const void *) &db,
2304 sizeof(db), sizeof(Dwarf_Ubyte));
2305 data += sizeof(Dwarf_Ubyte);
2306 memcpy((void *) data,
2307 (const void *) curinst->dfp_args,
2308 curinst->dfp_nbytes);
2309 data += curinst->dfp_nbytes;
2310 curinst = curinst->dfp_next;
2311 }
2312 }
2313 /* padding */
2314 for (i = 0; i < pad2; i++) {
2315 *data = DW_CFA_nop;
2316 data++;
2317 }
2318 cur_off += fde_length + offset_size;
2319 curfde = curfde->fde_next;
2320 }
2321
2322 *nbufs = dbg->de_n_debug_sect;
2323 return DW_DLV_OK;
2324 }
2325
2326 /*
2327 These functions remember all the markers we see along
2328 with the right offset in the .debug_info section so that
2329 we can dump them all back to the user with the section info.
2330 */
2331
2332 static int
2333 marker_init(Dwarf_P_Debug dbg,
2334 unsigned count)
2335 {
2336 dbg->de_marker_n_alloc = count;
2337 dbg->de_markers = NULL;
2338 if (count > 0) {
2339 dbg->de_markers = _dwarf_p_get_alloc(dbg,
2340 sizeof(struct Dwarf_P_Marker_s) * dbg->de_marker_n_alloc);
2341 if (dbg->de_markers == NULL) {
2342 dbg->de_marker_n_alloc = 0;
2343 return DW_DLV_ERROR;
2344 }
2345 }
2346 return DW_DLV_OK;
2347 }
2348
2349 static int
2350 marker_add(Dwarf_P_Debug dbg,
2351 Dwarf_Unsigned offset,
2352 Dwarf_Unsigned marker)
2353 {
2354 if (dbg->de_marker_n_alloc >= (dbg->de_marker_n_used + 1)) {
2355 unsigned n = dbg->de_marker_n_used++;
2356 dbg->de_markers[n].ma_offset = offset;
2357 dbg->de_markers[n].ma_marker = marker;
2358 return DW_DLV_OK;
2359 }
2360 return DW_DLV_ERROR;
2361 }
2362
2363 int
2364 dwarf_get_die_markers_a(Dwarf_P_Debug dbg,
2365 Dwarf_P_Marker * marker_list, /* pointer to a pointer */
2366 Dwarf_Unsigned * marker_count,
2367 Dwarf_Error * error)
2368 {
2369 if (marker_list == NULL || marker_count == NULL) {
2370 DWARF_P_DBG_ERROR(dbg, DW_DLE_IA, DW_DLV_ERROR);
2371 }
2372 if (dbg->de_marker_n_used != dbg->de_marker_n_alloc) {
2373 DWARF_P_DBG_ERROR(dbg, DW_DLE_MAF, DW_DLV_ERROR);
2374 }
2375
2376 *marker_list = dbg->de_markers;
2377 *marker_count = dbg->de_marker_n_used;
2378 return DW_DLV_OK;
2379 }
2380
2381 /* These functions provide the offsets of DW_FORM_string
2382 attributes in the section section_index. These information
2383 will enable a producer app that is generating assembly
2384 text output to easily emit those attributes in ascii form
2385 without having to decode the byte stream. */
2386 static int
2387 string_attr_init (Dwarf_P_Debug dbg,
2388 Dwarf_Signed section_index,
2389 unsigned count)
2390 {
2391 Dwarf_P_Per_Sect_String_Attrs sect_sa =
2392 &dbg->de_sect_string_attr[section_index];
2393
2394 sect_sa->sect_sa_n_alloc = count;
2395 sect_sa->sect_sa_list = NULL;
2396 if (count > 0) {
2397 sect_sa->sect_sa_section_number = section_index;
2398 sect_sa->sect_sa_list = _dwarf_p_get_alloc(dbg,
2399 sizeof(struct Dwarf_P_String_Attr_s) *
2400 sect_sa->sect_sa_n_alloc);
2401 if (sect_sa->sect_sa_list == NULL) {
2402 sect_sa->sect_sa_n_alloc = 0;
2403 return DW_DLV_ERROR;
2404 }
2405 }
2406 return DW_DLV_OK;
2407 }
2408
2409 static int
2410 string_attr_add (Dwarf_P_Debug dbg,
2411 Dwarf_Signed section_index,
2412 Dwarf_Unsigned offset,
2413 Dwarf_P_Attribute attr)
2414 {
2415 Dwarf_P_Per_Sect_String_Attrs sect_sa =
2416 &dbg->de_sect_string_attr[section_index];
2417 if (sect_sa->sect_sa_n_alloc >= (sect_sa->sect_sa_n_used + 1)) {
2418 unsigned n = sect_sa->sect_sa_n_used++;
2419 sect_sa->sect_sa_list[n].sa_offset = offset;
2420 sect_sa->sect_sa_list[n].sa_nbytes = attr->ar_nbytes;
2421 return DW_DLV_OK;
2422 }
2423
2424 return DW_DLV_ERROR;
2425 }
2426
2427 int
2428 dwarf_get_string_attributes_count(Dwarf_P_Debug dbg,
2429 Dwarf_Unsigned *
2430 count_of_sa_sections,
2431 int *drd_buffer_version,
2432 Dwarf_Error *error)
2433 {
2434 int i = 0;
2435 unsigned int count = 0;
2436
2437 (void)error;
2438 for (i = 0; i < NUM_DEBUG_SECTIONS; ++i) {
2439 if (dbg->de_sect_string_attr[i].sect_sa_n_used > 0) {
2440 ++count;
2441 }
2442 }
2443 *count_of_sa_sections = (Dwarf_Unsigned) count;
2444 *drd_buffer_version = DWARF_DRD_BUFFER_VERSION;
2445
2446 return DW_DLV_OK;
2447 }
2448
2449 int
2450 dwarf_get_string_attributes_info(Dwarf_P_Debug dbg,
2451 Dwarf_Signed *elf_section_index,
2452 Dwarf_Unsigned *sect_sa_buffer_count,
2453 Dwarf_P_String_Attr *sect_sa_buffer,
2454 Dwarf_Error *error)
2455 {
2456 int i = 0;
2457 int next = dbg->de_sect_sa_next_to_return;
2458
2459 (void)error;
2460 for (i = next; i < NUM_DEBUG_SECTIONS; ++i) {
2461 Dwarf_P_Per_Sect_String_Attrs sect_sa =
2462 &dbg->de_sect_string_attr[i];
2463 if (sect_sa->sect_sa_n_used > 0) {
2464 dbg->de_sect_sa_next_to_return = i + 1;
2465 *elf_section_index = sect_sa->sect_sa_section_number;
2466 *sect_sa_buffer_count = sect_sa->sect_sa_n_used;
2467 *sect_sa_buffer = sect_sa->sect_sa_list;
2468 return DW_DLV_OK;
2469 }
2470 }
2471 return DW_DLV_NO_ENTRY;
2472 }
2473
2474 static int
2475 has_sibling_die_already(Dwarf_P_Die d)
2476 {
2477 Dwarf_P_Attribute a = 0;
2478 for (a = d->di_attrs; a ; a = a->ar_next) {
2479 if (a->ar_attribute == DW_AT_sibling) {
2480 return TRUE;
2481 }
2482 }
2483 return FALSE;
2484 }
2485
2486 /* For DW_FORM_strp we need to set the symindex so we need
2487 to check that such applies. */
2488 static int
2489 if_relocatable_string_form(Dwarf_P_Debug dbg,
2490 Dwarf_P_Attribute curattr,
2491 int *debug_str_reloc,
2492 Dwarf_Error *error)
2493 {
2494 if (curattr->ar_rel_type == R_MIPS_NONE) {
2495 *debug_str_reloc = 0;
2496 return DW_DLV_OK;
2497 }
2498 if (curattr->ar_attribute_form != DW_FORM_strp) {
2499 _dwarf_p_error(dbg, error,DW_DLE_DEBUGSTR_UNEXPECTED_REL);
2500 return DW_DLV_ERROR;
2501 }
2502 if (curattr->ar_rel_type != dbg->de_offset_reloc) {
2503 _dwarf_p_error(dbg, error,DW_DLE_DEBUGSTR_UNEXPECTED_REL);
2504 return DW_DLV_ERROR;
2505 }
2506 *debug_str_reloc = 1;
2507 return DW_DLV_OK;
2508 }
2509
2510 /* Tries to see if given attribute and form combination
2511 of the attr exists in the given abbreviation.
2512 abbrevs and attrs are sorted in attrnum order. */
2513 static int
2514 _dwarf_pro_match_attr(Dwarf_P_Attribute attr,
2515 Dwarf_P_Abbrev abbrev, int no_attr)
2516 {
2517 int i = 0;
2518 Dwarf_P_Attribute curatp = attr;
2519
2520 for (i = 0; i < no_attr && curatp;
2521 i++,curatp = curatp->ar_next ) {
2522 if (curatp->ar_attribute != abbrev->abb_attrs[i] ||
2523 curatp->ar_attribute_form != abbrev->abb_forms[i]) {
2524 return 0;
2525 }
2526 /* If either is implicit_const need special
2527 check for matching val. */
2528 if (curatp->ar_attribute_form == DW_FORM_implicit_const) {
2529 if (abbrev->abb_forms[i] == DW_FORM_implicit_const) {
2530 if (curatp->ar_implicit_const !=
2531 abbrev->abb_implicits[i]) {
2532 return 0;
2533 }
2534 } else {
2535 return 0;
2536 }
2537 } else {
2538 if (abbrev->abb_forms[i] == DW_FORM_implicit_const) {
2539 return 0;
2540 }
2541 }
2542 }
2543 return 1;
2544 }
2545
2546 static int
2547 verify_ab_no_dups(struct Dwarf_Sort_Abbrev_s *sortab,
2548 int attrcount)
2549 {
2550 int k = 0;
2551 unsigned preva = 0;
2552 struct Dwarf_Sort_Abbrev_s *ab = sortab;
2553
2554 if (attrcount < 2) {
2555 return DW_DLV_OK;
2556 }
2557 for (k = 0; k < attrcount; ++k,++ab) {
2558 if (k) {
2559 if (preva >= ab->dsa_attr) {
2560 return DW_DLV_ERROR;
2561 }
2562 }
2563 preva = ab->dsa_attr;
2564 }
2565 return DW_DLV_OK;
2566 }
2567
2568 static int
2569 abcompare(const void *l_in, const void *r_in)
2570 {
2571 struct Dwarf_Sort_Abbrev_s *l =
2572 (struct Dwarf_Sort_Abbrev_s *)l_in;
2573 struct Dwarf_Sort_Abbrev_s *r =
2574 (struct Dwarf_Sort_Abbrev_s *)r_in;
2575 if (l->dsa_attr < r->dsa_attr) {
2576 return -1;
2577 }
2578 if (l->dsa_attr > r->dsa_attr) {
2579 return +1;
2580 }
2581 /* ASSERT: This never happens in correct dwarf. */
2582 return 0;
2583 }
2584
2585 /* Handles abbreviations. It takes a die, searches through
2586 current list of abbreviations for a matching one. If it
2587 finds one, it returns a pointer to the abbrev through
2588 the ab_out pointer, and if it does not,
2589 it returns a new abbrev through the ab_out pointer.
2590
2591 The die->die_attrs are sorted by attribute and the curabbrev
2592 attrs are too.
2593
2594 It is up to the user of this function to
2595 link it up to the abbreviation head. If it is a new abbrev
2596 abb_idx has 0. */
2597 static int
2598 _dwarf_pro_getabbrev(Dwarf_P_Debug dbg,
2599 Dwarf_P_Die die, Dwarf_P_Abbrev head,
2600 Dwarf_P_Abbrev*ab_out,Dwarf_Error *error)
2601 {
2602 Dwarf_P_Abbrev curabbrev = 0;
2603 Dwarf_P_Attribute curattr = 0;
2604 int match = 0;
2605 Dwarf_Unsigned *forms = 0;
2606 Dwarf_Unsigned *attrs = 0;
2607 Dwarf_Signed *implicits = 0;
2608 int attrcount = die->di_n_attr;
2609
2610 curabbrev = head;
2611 /* Loop thru the currently known abbreviations needed
2612 to see if we can share an existing abbrev. */
2613 while (curabbrev) {
2614 if ((die->di_tag == curabbrev->abb_tag) &&
2615 ((die->di_child != NULL &&
2616 curabbrev->abb_children == DW_CHILDREN_yes) ||
2617 (die->di_child == NULL &&
2618 curabbrev->abb_children == DW_CHILDREN_no)) &&
2619 (attrcount == curabbrev->abb_n_attr)) {
2620
2621 /* There is a chance of a match, basic
2622 characterists match. Now Check the attrs and
2623 forms. */
2624 curattr = die->di_attrs;
2625 match = _dwarf_pro_match_attr(curattr,
2626 curabbrev,
2627 (int) curabbrev->abb_n_attr);
2628 if (match == 1) {
2629 /* This tag/children/abbrev-list matches
2630 the incoming die needs exactly. Reuse
2631 this abbreviation. */
2632 *ab_out = curabbrev;
2633 return DW_DLV_OK;
2634 }
2635 }
2636 curabbrev = curabbrev->abb_next;
2637 }
2638 /* no match, create new abbreviation */
2639 if (attrcount) {
2640 forms = (Dwarf_Unsigned *)
2641 _dwarf_p_get_alloc(die->di_dbg,
2642 sizeof(Dwarf_Unsigned) * attrcount);
2643 if (forms == NULL) {
2644 DWARF_P_DBG_ERROR(dbg, DW_DLE_ABBREV_ALLOC, DW_DLV_ERROR);
2645 }
2646 attrs = (Dwarf_Unsigned *)
2647 _dwarf_p_get_alloc(die->di_dbg,
2648 sizeof(Dwarf_Unsigned) * attrcount);
2649 if (attrs == NULL) {
2650 DWARF_P_DBG_ERROR(dbg, DW_DLE_ABBREV_ALLOC, DW_DLV_ERROR);
2651 }
2652 implicits = (Dwarf_Signed *)
2653 _dwarf_p_get_alloc(die->di_dbg,
2654 sizeof(Dwarf_Signed) * attrcount);
2655 if (implicits == NULL) {
2656 DWARF_P_DBG_ERROR(dbg, DW_DLE_ABBREV_ALLOC, DW_DLV_ERROR);
2657 }
2658 }
2659 curattr = die->di_attrs;
2660 if (forms && attrs && attrcount) {
2661 struct Dwarf_Sort_Abbrev_s *sortab = 0;
2662 struct Dwarf_Sort_Abbrev_s *ap = 0;
2663 int k = 0;
2664 int res = 0;
2665
2666 sortab = (struct Dwarf_Sort_Abbrev_s *)
2667 malloc(sizeof(struct Dwarf_Sort_Abbrev_s)*attrcount);
2668 if (!sortab) {
2669 DWARF_P_DBG_ERROR(dbg, DW_DLE_ABBREV_ALLOC, DW_DLV_ERROR);
2670 }
2671 /* ASSERT curattr->ar_next chain length == attrcount */
2672 ap = sortab;
2673 for ( ; curattr; ++ap, curattr = curattr->ar_next) {
2674 ap->dsa_attr = curattr->ar_attribute;
2675 ap->dsa_form = curattr->ar_attribute_form;
2676 ap->dsa_implicitvalue = curattr->ar_implicit_const;
2677 ap->dsa_attrp = 0;
2678 }
2679 ap = sortab;
2680 if (attrcount > 1) {
2681 qsort(ap,attrcount,sizeof(struct Dwarf_Sort_Abbrev_s),
2682 abcompare);
2683 k = 0;
2684 res = verify_ab_no_dups(sortab,attrcount);
2685 if (res != DW_DLV_OK) {
2686 free(sortab);
2687 DWARF_P_DBG_ERROR(dbg,DW_DLE_DUP_ATTR_ON_DIE,
2688 DW_DLV_ERROR);
2689 }
2690 }
2691 for ( ; k < attrcount; ++k,++ap) {
2692 attrs[k] = ap->dsa_attr;
2693 forms[k] = ap->dsa_form;
2694 implicits[k] = ap->dsa_implicitvalue;
2695 }
2696 free(sortab);
2697 }
2698
2699 curabbrev = (Dwarf_P_Abbrev)
2700 _dwarf_p_get_alloc(die->di_dbg,
2701 sizeof(struct Dwarf_P_Abbrev_s));
2702 if (curabbrev == NULL) {
2703 DWARF_P_DBG_ERROR(dbg, DW_DLE_ABBREV_ALLOC, DW_DLV_ERROR);
2704 }
2705
2706 if (die->di_child == NULL) {
2707 curabbrev->abb_children = DW_CHILDREN_no;
2708 } else {
2709 curabbrev->abb_children = DW_CHILDREN_yes;
2710 }
2711 curabbrev->abb_tag = die->di_tag;
2712 curabbrev->abb_attrs = attrs;
2713 curabbrev->abb_forms = forms;
2714 curabbrev->abb_implicits = implicits;
2715 curabbrev->abb_n_attr = attrcount;
2716 curabbrev->abb_idx = 0;
2717 curabbrev->abb_next = NULL;
2718 *ab_out = curabbrev;
2719 return DW_DLV_OK;
2720 }
2721
2722 /* Generate debug_info and debug_abbrev sections */
2723 /* DWARF 2,3,4 */
2724 static int
2725 generate_debuginfo_header_2(Dwarf_P_Debug dbg,
2726 unsigned *abbrev_offset_io,
2727 unsigned char **data_io,
2728 int *cu_header_size_out,
2729 Dwarf_Small **abbr_off_ptr_out,
2730 Dwarf_Half version,
2731 int extension_size,
2732 Dwarf_Ubyte address_size,
2733 Dwarf_Error * error)
2734 {
2735 unsigned abbrev_offset = 0;
2736 unsigned char * data = 0;
2737 int offset_size = dbg->de_dwarf_offset_size;
2738 int elfsectno_of_debug_info = dbg->de_elf_sects[DEBUG_INFO];
2739 int cu_header_size = 0;
2740 Dwarf_Unsigned du = 0;
2741 Dwarf_Small *abbr_off_ptr = 0;
2742
2743 /* write cu header. abbrev_offset used to
2744 generate relocation record below */
2745 abbrev_offset = OFFSET_PLUS_EXTENSION_SIZE +
2746 DWARF_HALF_SIZE ;
2747
2748 cu_header_size = abbrev_offset +
2749 offset_size + sizeof(Dwarf_Ubyte);
2750
2751 GET_CHUNK_ERR(dbg, elfsectno_of_debug_info, data, cu_header_size,
2752 error);
2753 if (extension_size) {
2754 /* This for a dwarf-standard 64bit offset. */
2755 DISTINGUISHED_VALUE_ARRAY(v4);
2756
2757 WRITE_UNALIGNED(dbg, (void *) data,
2758 (const void *) &v4[0], SIZEOFT32, extension_size);
2759 data += extension_size;
2760 }
2761 abbr_off_ptr = data;
2762 du = 0; /* length of debug_info, not counting
2763 this field itself (unknown at this point). */
2764 WRITE_UNALIGNED(dbg, (void *) data,
2765 (const void *) &du, sizeof(du), offset_size);
2766 data += offset_size;
2767
2768 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &version,
2769 sizeof(version), DWARF_HALF_SIZE);
2770 data += DWARF_HALF_SIZE;
2771
2772 du = 0;/* offset into abbrev table, not yet known. */
2773 WRITE_UNALIGNED(dbg, (void *) data,
2774 (const void *) &du, sizeof(du), offset_size);
2775 data += offset_size;
2776
2777 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &address_size,
2778 sizeof(address_size), sizeof(Dwarf_Ubyte));
2779 data += sizeof(Dwarf_Ubyte);
2780
2781 /* We have filled the chunk we got with GET_CHUNK.
2782 At this point we
2783 no longer dare use "data" as a
2784 pointer any longer except to refer to that first
2785 small chunk for the cu header to update
2786 the section length. */
2787 *abbrev_offset_io = abbrev_offset;
2788 *data_io = data;
2789 *cu_header_size_out = cu_header_size;
2790 *abbr_off_ptr_out = abbr_off_ptr;
2791 return DW_DLV_OK;
2792 }
2793
2794 /* DWARF 5 */
2795 static int
2796 generate_debuginfo_header_5(Dwarf_P_Debug dbg,
2797 unsigned *abbrev_offset_io,
2798 unsigned char **data_io,
2799 int *cu_header_size_out,
2800 Dwarf_Small **abbr_off_ptr_out,
2801 Dwarf_Half version,
2802 Dwarf_Ubyte unit_type,
2803 int extension_size,
2804 Dwarf_Ubyte address_size,
2805 Dwarf_Error *error)
2806 {
2807 int offset_size = dbg->de_dwarf_offset_size;
2808 unsigned abbrev_offset = 0;
2809 unsigned char * data = 0;
2810 int elfsectno_of_debug_info = dbg->de_elf_sects[DEBUG_INFO];
2811 int cu_header_size = 0;
2812 Dwarf_Unsigned du = 0;
2813 Dwarf_Small *abbr_off_ptr = 0;
2814
2815 /* write cu header. abbrev_offset used to
2816 generate relocation record below */
2817 abbrev_offset = OFFSET_PLUS_EXTENSION_SIZE +
2818 DWARF_HALF_SIZE + /* version stamp */
2819 sizeof(unit_type) +
2820 sizeof(Dwarf_Ubyte);
2821 cu_header_size = abbrev_offset + offset_size;
2822
2823 GET_CHUNK_ERR(dbg, elfsectno_of_debug_info, data, cu_header_size,
2824 error);
2825 if (extension_size) {
2826 /* Impossible in DW5, really, is for IRIX64.
2827 But we allow it. */
2828 DISTINGUISHED_VALUE_ARRAY(v4);
2829
2830 WRITE_UNALIGNED(dbg, (void *) data,
2831 (const void *) &v4[0], SIZEOFT32, extension_size);
2832 data += extension_size;
2833 }
2834 abbr_off_ptr = data;
2835 du = 0; /* length of debug_info, not counting
2836 this field itself (unknown at this point). */
2837 WRITE_UNALIGNED(dbg, (void *) data,
2838 (const void *) &du, sizeof(du), offset_size);
2839 data += offset_size;
2840
2841 WRITE_UNALIGNED(dbg, (void *) data,
2842 (const void *) &version,
2843 sizeof(version), DWARF_HALF_SIZE);
2844 data += DWARF_HALF_SIZE;
2845
2846 WRITE_UNALIGNED(dbg, (void *) data,
2847 (const void *) &unit_type,
2848 sizeof(unit_type), sizeof(Dwarf_Ubyte));
2849 data += sizeof(Dwarf_Ubyte);
2850
2851 WRITE_UNALIGNED(dbg, (void *) data, (const void *) &address_size,
2852 sizeof(address_size), sizeof(Dwarf_Ubyte));
2853 data += sizeof(Dwarf_Ubyte);
2854
2855 du = 0;/* offset into abbrev table, not yet known. */
2856 WRITE_UNALIGNED(dbg, (void *) data,
2857 (const void *) &du, sizeof(du), offset_size);
2858 data += offset_size;
2859
2860 /* We have filled the chunk we got with GET_CHUNK.
2861 At this point we
2862 no longer dare use "data" as a pointer any
2863 longer except to refer to that first small chunk for the cu
2864 header to update the section length. */
2865
2866 *abbrev_offset_io = abbrev_offset;
2867 *data_io = data;
2868 *cu_header_size_out = cu_header_size;
2869 *abbr_off_ptr_out = abbr_off_ptr;
2870 return DW_DLV_OK;
2871 }
2872
2873 /* Write out debug_abbrev section */
2874 static int
2875 write_out_debug_abbrev(Dwarf_P_Debug dbg,
2876 Dwarf_P_Abbrev abbrev_head,
2877 Dwarf_Error * error)
2878 {
2879 Dwarf_P_Abbrev curabbrev = abbrev_head;
2880 unsigned char *data = 0;
2881 int res = 0;
2882 int abbrevsectno = dbg->de_elf_sects[DEBUG_ABBREV];
2883
2884 while (curabbrev) {
2885 int idx = 0;
2886 unsigned lebcount = 0;
2887 Dwarf_Ubyte db = 0;
2888
2889 res = write_uval(curabbrev->abb_idx,dbg,abbrevsectno,
2890 &lebcount,error);
2891 if (res != DW_DLV_OK) {
2892 return res;
2893 }
2894
2895 res = write_uval(curabbrev->abb_tag,dbg,abbrevsectno,
2896 &lebcount,error);
2897 if (res != DW_DLV_OK) {
2898 return res;
2899 }
2900
2901 db = curabbrev->abb_children;
2902 res = write_ubyte(db,dbg,abbrevsectno,&lebcount,error);
2903 if (res != DW_DLV_OK) {
2904 return res;
2905 }
2906
2907 /* add attributes and forms */
2908 for (idx = 0; idx < curabbrev->abb_n_attr; idx++) {
2909 res =write_uval(curabbrev->abb_attrs[idx],
2910 dbg,abbrevsectno,
2911 &lebcount,error);
2912 if (res != DW_DLV_OK) {
2913 return res;
2914 }
2915 res =write_uval(curabbrev->abb_forms[idx],
2916 dbg,abbrevsectno,
2917 &lebcount,error);
2918 if (res != DW_DLV_OK) {
2919 return res;
2920 }
2921 if (curabbrev->abb_forms[idx] == DW_FORM_implicit_const){
2922 res =write_sval(curabbrev->abb_implicits[idx],
2923 dbg,abbrevsectno,
2924 &lebcount,error);
2925 if (res != DW_DLV_OK) {
2926 return res;
2927 }
2928 }
2929 }
2930 /* Two zeros, for last entry, see dwarf2 sec 7.5.3 */
2931 GET_CHUNK_ERR(dbg, abbrevsectno, data, 2, error);
2932 *data = 0;
2933 data++;
2934 *data = 0;
2935
2936 curabbrev = curabbrev->abb_next;
2937 }
2938 /* one zero, for end of cu, see dwarf2 sec 7.5.3 */
2939 GET_CHUNK_ERR(dbg, abbrevsectno, data, 1, error);
2940 *data = 0;
2941 return DW_DLV_OK;
2942 }
2943
2944 static int
2945 sort_die_attrs(Dwarf_P_Debug dbg,Dwarf_P_Die die,
2946 Dwarf_Error *error)
2947 {
2948 struct Dwarf_Sort_Abbrev_s *sortab = 0;
2949 struct Dwarf_Sort_Abbrev_s *ap = 0;
2950 Dwarf_P_Attribute at = 0;
2951 Dwarf_P_Attribute sorted_attrlist = 0;
2952 Dwarf_P_Attribute sorted_tail = 0;
2953 int attrcount = die->di_n_attr;
2954 int res = 0;
2955 int k = 0;
2956
2957 if (attrcount < 2) {
2958 return DW_DLV_OK;
2959 }
2960 sortab = (struct Dwarf_Sort_Abbrev_s *)
2961 malloc(sizeof(struct Dwarf_Sort_Abbrev_s)*attrcount);
2962 if (!sortab) {
2963 DWARF_P_DBG_ERROR(dbg, DW_DLE_ABBREV_ALLOC, DW_DLV_ERROR);
2964 }
2965 /* ASSERT at->ar_next chain length == attrcount */
2966 ap = sortab;
2967 at = die->di_attrs;
2968 for ( ; at; ++ap, at = at->ar_next) {
2969 ap->dsa_attr = at->ar_attribute;
2970 ap->dsa_form = at->ar_attribute_form;
2971 ap->dsa_attrp = at;
2972 }
2973 qsort(sortab,attrcount,sizeof(struct Dwarf_Sort_Abbrev_s),
2974 abcompare);
2975 res = verify_ab_no_dups(sortab,attrcount);
2976 if (res != DW_DLV_OK) {
2977 free(sortab);
2978 DWARF_P_DBG_ERROR(dbg, DW_DLE_DUP_ATTR_ON_DIE, DW_DLV_ERROR);
2979 }
2980 ap = sortab;
2981 k = 0;
2982 for ( ; k < attrcount; ++k,++ap) {
2983 Dwarf_P_Attribute localptr = ap->dsa_attrp;
2984 if (!sorted_attrlist) {
2985 sorted_attrlist = localptr;
2986 sorted_tail = sorted_attrlist;
2987 localptr->ar_next = 0;
2988 continue;
2989 }
2990 sorted_tail->ar_next = localptr;
2991 sorted_tail = localptr;
2992 localptr->ar_next = 0;
2993 }
2994 /* Now replace the list with the same pointers
2995 but in order sorted by attribute. */
2996 die->di_attrs = sorted_attrlist;
2997 free(sortab);
2998 return DW_DLV_OK;
2999 }
3000
3001 static int
3002 _dwarf_pro_generate_debugsup(Dwarf_P_Debug dbg,
3003 Dwarf_Unsigned *nbufs,
3004 Dwarf_Error * error)
3005 {
3006 int elfsectno = 0;
3007 unsigned char *data = 0;
3008 Dwarf_Half version = 0;
3009 unsigned int uleblen = 0;
3010 int leblen = 0;
3011 unsigned i = 0;
3012 unsigned name_len = 0;
3013 Dwarf_Unsigned alloc_size = 0;
3014 int res = 0;
3015
3016 elfsectno = dbg->de_elf_sects[DEBUG_SUP];
3017 res = pretend_write_uval(dbg->de_debug_sup.ds_checksum_len,
3018 dbg, &uleblen,error);
3019 if (res != DW_DLV_OK) {
3020 return res;
3021 }
3022 version = dbg->de_debug_sup.ds_version;
3023 name_len = strlen(dbg->de_debug_sup.ds_filename) +1;
3024 alloc_size = DWARF_HALF_SIZE +
3025 1 + name_len +
3026 uleblen + dbg->de_debug_sup.ds_checksum_len;
3027 GET_CHUNK_ERR(dbg, elfsectno, data, alloc_size, error);
3028
3029 WRITE_UNALIGNED(dbg, (void *) data,
3030 (const void *)&version, DWARF_HALF_SIZE, DWARF_HALF_SIZE);
3031 data += DWARF_HALF_SIZE;
3032
3033 *data = dbg->de_debug_sup.ds_is_supplementary;
3034 ++data;
3035
3036 for (i = 0 ; i < name_len ; ++i,++data) {
3037 *data = *(unsigned char *)(dbg->de_debug_sup.ds_filename +i);
3038 }
3039
3040 res = dwarf_encode_leb128(
3041 dbg->de_debug_sup.ds_checksum_len,
3042 &leblen,
3043 (char *)data,(int)uleblen);
3044 if (res != DW_DLV_OK) {
3045 DWARF_P_DBG_ERROR(dbg, DW_DLE_DEBUG_SUP_ERROR, DW_DLV_ERROR);
3046 }
3047 data += uleblen;
3048
3049 memcpy(data,dbg->de_debug_sup.ds_checksum,
3050 dbg->de_debug_sup.ds_checksum_len);
3051 *nbufs = dbg->de_n_debug_sect;
3052 return DW_DLV_OK;
3053 }
3054
3055 static int
3056 _dwarf_pro_generate_debuginfo(Dwarf_P_Debug dbg,
3057 Dwarf_Unsigned *nbufs,
3058 Dwarf_Error * error)
3059 {
3060 int elfsectno_of_debug_info = 0;
3061 unsigned char *data = 0;
3062 int cu_header_size = 0;
3063 Dwarf_P_Abbrev curabbrev = 0;
3064 Dwarf_P_Abbrev abbrev_head = 0;
3065 Dwarf_P_Abbrev abbrev_tail = 0;
3066 Dwarf_P_Die curdie = 0;
3067 Dwarf_P_Die first_child = 0;
3068 Dwarf_Unsigned dw = 0;
3069 Dwarf_Unsigned du = 0;
3070 Dwarf_Half dh = 0;
3071 Dwarf_Unsigned die_off = 0; /* Offset of die in debug_info. */
3072 int n_abbrevs = 0;
3073 unsigned abbrev_offset = 0;
3074 int res = 0;
3075 unsigned marker_count = 0;
3076 unsigned string_attr_count = 0;
3077 unsigned string_attr_offset = 0;
3078 Dwarf_Small *abbr_off_ptr = 0;
3079
3080 int offset_size = dbg->de_dwarf_offset_size;
3081 /* extension_size is oddly names. The standard calls
3082 for a 64bit offset to have a 4 byte 0xffff
3083 while original IRIX64 did not.
3084 So if dbg->de_64bit_extension set this is a standard
3085 DWARF 64bit offset and if de_64bit_extension not set
3086 this is non-standard IRIX64 64 bit offset. */
3087 Dwarf_Half version = dbg->de_output_version;
3088 int extension_size = dbg->de_64bit_extension ? 4 : 0;
3089
3090 /* For now just assume DW_UT_compile FIXME */
3091 Dwarf_Ubyte unit_type = DW_UT_compile;
3092 Dwarf_Ubyte address_size = 0;
3093
3094 elfsectno_of_debug_info = dbg->de_elf_sects[DEBUG_INFO];
3095 address_size = dbg->de_pointer_size;
3096 if (version < 5) {
3097 res = generate_debuginfo_header_2(dbg,
3098 &abbrev_offset,
3099 &data,
3100 &cu_header_size,
3101 &abbr_off_ptr,
3102 version, extension_size, address_size,
3103 error);
3104 if (res != DW_DLV_OK) {
3105 return res;
3106 }
3107 } else if (version == 5) {
3108 res = generate_debuginfo_header_5(dbg,
3109 &abbrev_offset,
3110 &data,
3111 &cu_header_size,
3112 &abbr_off_ptr,
3113 version, unit_type, extension_size, address_size,
3114 error);
3115 if (res != DW_DLV_OK) {
3116 return res;
3117 }
3118 } else {
3119 DWARF_P_DBG_ERROR(dbg, DW_DLE_VERSION_STAMP_ERROR,
3120 DW_DLV_ERROR);
3121 }
3122
3123 curdie = dbg->de_dies;
3124
3125 /* Create AT_macro_info if appropriate */
3126 if (version < 5) {
3127 if (dbg->de_first_macinfo != NULL) {
3128 res = _dwarf_pro_add_AT_macro_info(dbg, curdie, 0, error);
3129 if (res != DW_DLV_OK) {
3130 return res;
3131 }
3132 }
3133 } else {
3134 /* FIXME need to add code to emit DWARF5 macro data. */
3135 #if 0
3136 res = _dwarf_pro_add_AT_macro5_info(dbg, curdie,
3137 0, error);
3138 #endif
3139 }
3140
3141 /* Create AT_stmt_list attribute if necessary */
3142 if (dwarf_need_debug_line_section(dbg) == TRUE) {
3143 res =_dwarf_pro_add_AT_stmt_list(dbg, curdie, error);
3144 if (res != DW_DLV_OK) {
3145 return res;
3146 }
3147 }
3148 die_off = cu_header_size;
3149
3150 /* Relocation for abbrev offset in cu header store relocation
3151 record in linked list */
3152 res = dbg->de_relocate_by_name_symbol(dbg,
3153 DEBUG_INFO,
3154 abbrev_offset /* r_offset */,
3155 dbg->de_sect_name_idx[DEBUG_ABBREV],
3156 dwarf_drt_data_reloc, offset_size);
3157 if (res != DW_DLV_OK) {
3158 DWARF_P_DBG_ERROR(dbg, DW_DLE_REL_ALLOC, DW_DLV_ERROR);
3159 }
3160
3161 /* Pass 0: only top level dies, add at_sibling attribute to those
3162 dies with children, but if and only if
3163 there is no sibling attribute already. */
3164 first_child = curdie->di_child;
3165 while (first_child && first_child->di_right) {
3166 if (first_child->di_child) {
3167 if (!has_sibling_die_already(first_child)) {
3168 Dwarf_P_Attribute attr_out = 0;
3169 res = dwarf_add_AT_reference_c(dbg,
3170 first_child,
3171 DW_AT_sibling,
3172 first_child->di_right,
3173 &attr_out,error);
3174 if (res != DW_DLV_OK) {
3175 /* DW_DLV_NO_ENTRY is impossible. */
3176 return res;
3177 }
3178 }
3179 }
3180 first_child = first_child->di_right;
3181 }
3182
3183 /* Pass 1: create abbrev info, get die offsets,
3184 calc relocations */
3185 abbrev_head = abbrev_tail = NULL;
3186 marker_count = 0;
3187 string_attr_count = 0;
3188 while (curdie != NULL) {
3189 int nbytes = 0;
3190 Dwarf_P_Attribute curattr = 0;
3191 char *space = 0;
3192 int cres = 0;
3193 char buff1[ENCODE_SPACE_NEEDED];
3194
3195 curdie->di_offset = die_off;
3196
3197 if (curdie->di_marker != 0) {
3198 marker_count++;
3199 }
3200 cres =sort_die_attrs(dbg,curdie,error);
3201 if (cres != DW_DLV_OK) {
3202 /* DW_DLV_NO_ENTRY is impossible. */
3203 return cres;
3204 }
3205 /* Find or create a final abbrev record for the
3206 debug_abbrev section we will write (below). */
3207 cres = _dwarf_pro_getabbrev(dbg,curdie,
3208 abbrev_head,&curabbrev,error);
3209 if (cres != DW_DLV_OK) {
3210 return cres;
3211 }
3212 if (abbrev_head == NULL) {
3213 n_abbrevs = 1;
3214 curabbrev->abb_idx = n_abbrevs;
3215 abbrev_tail = abbrev_head = curabbrev;
3216 } else {
3217 /* Check if it is a new abbreviation,
3218 if yes, add to tail */
3219 if (curabbrev->abb_idx == 0) {
3220 n_abbrevs++;
3221 curabbrev->abb_idx = n_abbrevs;
3222 abbrev_tail->abb_next = curabbrev;
3223 abbrev_tail = curabbrev;
3224 }
3225 }
3226 /* We know the abbrev number to use now.
3227 So create the bytes of the leb with the
3228 value and save those bytes in di_abbrev,
3229 we will emit in Pass 2 (below). */
3230 cres = dwarf_encode_leb128(curabbrev->abb_idx,
3231 &nbytes,
3232 buff1, sizeof(buff1));
3233 if (cres != DW_DLV_OK) {
3234 DWARF_P_DBG_ERROR(dbg, DW_DLE_ABBREV_ALLOC, DW_DLV_ERROR);
3235 }
3236 space = _dwarf_p_get_alloc(dbg, nbytes);
3237 if (space == NULL) {
3238 DWARF_P_DBG_ERROR(dbg, DW_DLE_ABBREV_ALLOC, DW_DLV_ERROR);
3239 }
3240 memcpy(space, buff1, nbytes);
3241 curdie->di_abbrev = space;
3242 curdie->di_abbrev_nbytes = nbytes;
3243 die_off += nbytes;
3244
3245 /* The abbrev and DIE attr lists match, so the die
3246 abbrevs are in the correct order,
3247 curdie->di_attrs. */
3248
3249 /* Now we attach the attributes list to the die. */
3250 curattr = curdie->di_attrs;
3251
3252 while (curattr) {
3253 if (curattr->ar_rel_type != R_MIPS_NONE) {
3254 int rres=0;
3255 switch (curattr->ar_attribute) {
3256 case DW_AT_stmt_list:
3257 curattr->ar_rel_symidx =
3258 dbg->de_sect_name_idx[DEBUG_LINE];
3259 break;
3260 case DW_AT_MIPS_fde:
3261 curattr->ar_rel_symidx =
3262 dbg->de_sect_name_idx[DEBUG_FRAME];
3263 break;
3264 case DW_AT_macro_info:
3265 curattr->ar_rel_symidx =
3266 dbg->de_sect_name_idx[DEBUG_MACINFO];
3267 break;
3268 /* See also: pro_forms.c for same strings
3269 attribute list. */
3270 case DW_AT_comp_dir:
3271 case DW_AT_const_value:
3272 case DW_AT_linkage_name: /* DWARF5 */
3273 case DW_AT_MIPS_abstract_name:
3274 case DW_AT_MIPS_linkage_name:
3275 case DW_AT_name:
3276 case DW_AT_producer: {
3277 int is_debug_str = 0;
3278 int nres = if_relocatable_string_form(dbg,curattr,
3279 &is_debug_str,error);
3280 if (nres != DW_DLV_OK) {
3281 return res;
3282 }
3283 if (is_debug_str) {
3284 curattr->ar_rel_symidx =
3285 dbg->de_sect_name_idx[DEBUG_STR];
3286 }
3287 }
3288 break;
3289 default:
3290 break;
3291 }
3292 rres = dbg->de_relocate_by_name_symbol(dbg,
3293 DEBUG_INFO,
3294 die_off + curattr->ar_rel_offset,/* r_offset */
3295 curattr->ar_rel_symidx,
3296 dwarf_drt_data_reloc,
3297 curattr->ar_reloc_len);
3298
3299 if (rres != DW_DLV_OK) {
3300 DWARF_P_DBG_ERROR(dbg, DW_DLE_REL_ALLOC,
3301 DW_DLV_ERROR);
3302 }
3303 }
3304 if (curattr->ar_attribute_form == DW_FORM_string) {
3305 string_attr_count++;
3306 }
3307 die_off += curattr->ar_nbytes;
3308 curattr = curattr->ar_next;
3309 }
3310 /* Depth first access to all the DIEs. */
3311 if (curdie->di_child) {
3312 curdie = curdie->di_child;
3313 } else {
3314 while (curdie != NULL && curdie->di_right == NULL) {
3315 curdie = curdie->di_parent;
3316 /* See -nonrootsibling- below */
3317 if (curdie != NULL) {
3318 die_off++;
3319 }
3320 }
3321 if (curdie != NULL) {
3322 curdie = curdie->di_right;
3323 }
3324 }
3325 } /* end while (curdie != NULL), the per-die loop */
3326
3327 res = marker_init(dbg, marker_count);
3328 if (res == DW_DLV_ERROR) {
3329 DWARF_P_DBG_ERROR(dbg, DW_DLE_REL_ALLOC, DW_DLV_ERROR);
3330 }
3331 res = string_attr_init(dbg, DEBUG_INFO, string_attr_count);
3332 if (res != DW_DLV_OK) {
3333 DWARF_P_DBG_ERROR(dbg, DW_DLE_REL_ALLOC, DW_DLV_ERROR);
3334 }
3335
3336 /* Pass 2: Write out the die information Here 'data' is a
3337 temporary, one block for each GET_CHUNK.
3338 'data' is overused. */
3339 curdie = dbg->de_dies;
3340 while (curdie != NULL) {
3341 Dwarf_P_Attribute curattr;
3342
3343 if (curdie->di_marker != 0) {
3344 res = marker_add(dbg, curdie->di_offset,
3345 curdie->di_marker);
3346 if (res == DW_DLV_ERROR) {
3347 DWARF_P_DBG_ERROR(dbg, DW_DLE_REL_ALLOC,
3348 DW_DLV_ERROR);
3349 }
3350 }
3351
3352 /* Index to abbreviation table */
3353 GET_CHUNK_ERR(dbg, elfsectno_of_debug_info,
3354 data, curdie->di_abbrev_nbytes, error);
3355 memcpy((void *) data,
3356 (const void *) curdie->di_abbrev,
3357 curdie->di_abbrev_nbytes);
3358
3359 /* Attribute values - need to fill in all form attributes */
3360 curattr = curdie->di_attrs;
3361 string_attr_offset = curdie->di_offset +
3362 curdie->di_abbrev_nbytes;
3363 while (curattr) {
3364 GET_CHUNK_ERR(dbg, elfsectno_of_debug_info, data,
3365 (unsigned long) curattr->ar_nbytes, error);
3366 switch (curattr->ar_attribute_form) {
3367 case DW_FORM_ref1:
3368 {
3369 Dwarf_Ubyte db = 0;
3370 if (curattr->ar_ref_die->di_offset >
3371 (unsigned) 0xff) {
3372 DWARF_P_DBG_ERROR(dbg, DW_DLE_OFFSET_UFLW,
3373 DW_DLV_ERROR);
3374 }
3375 db = curattr->ar_ref_die->di_offset;
3376 WRITE_UNALIGNED(dbg, (void *) data,
3377 (const void *) &db,
3378 sizeof(db), sizeof(Dwarf_Ubyte));
3379 break;
3380 }
3381 case DW_FORM_ref2:
3382 {
3383 if (curattr->ar_ref_die->di_offset >
3384 (unsigned) 0xffff) {
3385 DWARF_P_DBG_ERROR(dbg, DW_DLE_OFFSET_UFLW,
3386 DW_DLV_ERROR);
3387 }
3388 dh = curattr->ar_ref_die->di_offset;
3389 WRITE_UNALIGNED(dbg, (void *) data,
3390 (const void *) &dh,
3391 sizeof(dh), DWARF_HALF_SIZE);
3392 break;
3393 }
3394 case DW_FORM_ref_addr:
3395 {
3396 /* curattr->ar_ref_die == NULL!
3397
3398 DW_FORM_ref_addr doesn't take a CU-offset.
3399 This is different than other refs.
3400 This value will be set by the user of the
3401 producer library using a relocation.
3402 No need to set a value here. */
3403 break;
3404
3405 }
3406 case DW_FORM_ref4:
3407 {
3408 if (curattr->ar_ref_die->di_offset >
3409 (unsigned) 0xffffffff) {
3410 DWARF_P_DBG_ERROR(dbg, DW_DLE_OFFSET_UFLW,
3411 DW_DLV_ERROR);
3412 }
3413 dw = (Dwarf_Unsigned)
3414 curattr->ar_ref_die->di_offset;
3415 WRITE_UNALIGNED(dbg, (void *) data,
3416 (const void *) &dw,
3417 sizeof(dw), DWARF_32BIT_SIZE);
3418 break;
3419 }
3420 case DW_FORM_ref8:
3421 du = curattr->ar_ref_die->di_offset;
3422 WRITE_UNALIGNED(dbg, (void *) data,
3423 (const void *) &du,
3424 sizeof(du), DWARF_64BIT_SIZE);
3425 break;
3426 case DW_FORM_ref_udata:
3427 { /* unsigned leb128 offset */
3428
3429 int nbytesx;
3430 char buff1[ENCODE_SPACE_NEEDED];
3431
3432 res =
3433 dwarf_encode_leb128(curattr->
3434 ar_ref_die->
3435 di_offset, &nbytesx,
3436 buff1,
3437 sizeof(buff1));
3438 if (res != DW_DLV_OK) {
3439 DWARF_P_DBG_ERROR(dbg, DW_DLE_ABBREV_ALLOC,
3440 DW_DLV_ERROR);
3441 }
3442 memcpy(data, buff1, nbytesx);
3443 break;
3444 }
3445 default:
3446 if (curattr->ar_nbytes) {
3447 memcpy((void *) data,
3448 (const void *) curattr->ar_data,
3449 curattr->ar_nbytes);
3450 }
3451 break;
3452 }
3453 if (curattr->ar_attribute_form == DW_FORM_string) {
3454 string_attr_add(dbg, DEBUG_INFO,
3455 string_attr_offset, curattr);
3456 }
3457 string_attr_offset += curattr->ar_nbytes;
3458 curattr = curattr->ar_next;
3459 }
3460
3461 /* depth first search */
3462 if (curdie->di_child) {
3463 curdie = curdie->di_child;
3464 } else {
3465 while (curdie != NULL && curdie->di_right == NULL) {
3466 /* -nonrootsibling-
3467 A null die should only be written for terminating
3468 siblings, not the root. Adding a terminating die
3469 for the root will cause, after object files are
3470 linked, warnings to be generated with newer
3471 versions of readelf. */
3472 if (!curdie->di_parent) {
3473 /* The parent is not a DIE so ending a sibling
3474 chain makes no sense (wastes a byte). */
3475 break;
3476 }
3477 GET_CHUNK_ERR(dbg, elfsectno_of_debug_info,
3478 data, 1, error);
3479 *data = '\0';
3480 curdie = curdie->di_parent;
3481 }
3482 if (curdie != NULL)
3483 curdie = curdie->di_right;
3484 }
3485 } /* end while (curdir != NULL) */
3486
3487 /* Write out debug_info size, now that we know it
3488 This is back-patching the CU header we created
3489 above. */
3490 du = die_off - OFFSET_PLUS_EXTENSION_SIZE;
3491 WRITE_UNALIGNED(dbg, (void *) abbr_off_ptr,
3492 (const void *) &du, sizeof(du), offset_size);
3493
3494 data = 0; /* Emphasize not usable now */
3495
3496 res = write_out_debug_abbrev(dbg,
3497 abbrev_head, error);
3498 if (res != DW_DLV_OK) {
3499 return res;
3500 }
3501
3502 *nbufs = dbg->de_n_debug_sect;
3503 return DW_DLV_OK;
3504 }
3505
3506 static int
3507 _dwarf_pro_generate_dnames(Dwarf_P_Debug dbg,
3508 Dwarf_Unsigned *nbufs,
3509 Dwarf_Error * error)
3510 {
3511 int elfsectno_of_debug_names = dbg->de_elf_sects[DEBUG_NAMES];
3512 if (dbg->de_force_dnames) {
3513 dwarf_force_dnames(dbg,elfsectno_of_debug_names,error);
3514 }
3515 *nbufs = dbg->de_n_debug_sect;
3516 return DW_DLV_OK;
3517 }
3518
3519 static int
3520 _dwarf_pro_generate_debug_str(Dwarf_P_Debug dbg,
3521 Dwarf_Unsigned *nbufs,
3522 Dwarf_Error * error)
3523 {
3524 int elfsectno_of_debug_str = 0;
3525 unsigned char *data = 0;
3526
3527 elfsectno_of_debug_str = dbg->de_elf_sects[DEBUG_STR];
3528 GET_CHUNK(dbg, elfsectno_of_debug_str, data,
3529 dbg->de_debug_str->ds_nbytes,error);
3530 memcpy(data,dbg->de_debug_str->ds_data,
3531 dbg->de_debug_str->ds_nbytes);
3532 *nbufs = dbg->de_n_debug_sect;
3533 return DW_DLV_OK;
3534 }
3535 static int
3536 _dwarf_pro_generate_debug_line_str(Dwarf_P_Debug dbg,
3537 Dwarf_Unsigned *nbufs,
3538 Dwarf_Error * error)
3539 {
3540 int elfsectno_of_debug_line_str = 0;
3541 unsigned char *data = 0;
3542
3543 elfsectno_of_debug_line_str = dbg->de_elf_sects[DEBUG_LINE_STR];
3544 GET_CHUNK(dbg, elfsectno_of_debug_line_str, data,
3545 dbg->de_debug_line_str->ds_nbytes,
3546 error);
3547 memcpy(data,dbg->de_debug_line_str->ds_data,
3548 dbg->de_debug_line_str->ds_nbytes);
3549 *nbufs = dbg->de_n_debug_sect;
3550 return DW_DLV_OK;
3551 }
3552
3553 /* Get a buffer of section data.
3554 section_idx is the elf-section number that this data applies to.
3555 length shows length of returned data
3556 */
3557 int
3558 dwarf_get_section_bytes_a(Dwarf_P_Debug dbg,
3559 Dwarf_Unsigned dwarf_section,
3560 Dwarf_Unsigned * section_idx,
3561 Dwarf_Unsigned * length,
3562 Dwarf_Ptr * section_bytes,
3563 Dwarf_Error * error)
3564 {
3565 Dwarf_Ptr buf = 0;
3566
3567 (void)dwarf_section;
3568 if (dbg->de_version_magic_number != PRO_VERSION_MAGIC) {
3569 DWARF_P_DBG_ERROR(dbg, DW_DLE_IA, DW_DLV_ERROR);
3570 }
3571 *section_bytes = 0;
3572 *length = 0;
3573 if (dbg->de_debug_sects == 0) {
3574 /* no more data !! */
3575 return DW_DLV_NO_ENTRY;
3576 }
3577 if (dbg->de_debug_sects->ds_elf_sect_no == MAGIC_SECT_NO) {
3578 /* no data ever entered !! */
3579 return DW_DLV_NO_ENTRY;
3580 }
3581 *section_idx = dbg->de_debug_sects->ds_elf_sect_no;
3582 *length = dbg->de_debug_sects->ds_nbytes;
3583
3584 buf = (Dwarf_Ptr *) dbg->de_debug_sects->ds_data;
3585
3586 /* Here is the iterator so the next call gets
3587 the next section. */
3588 dbg->de_debug_sects = dbg->de_debug_sects->ds_next;
3589 /* We may want to call the section stuff more than once: see
3590 dwarf_reset_section_bytes() do not do:
3591 dbg->de_n_debug_sect--; */
3592 *section_bytes = buf;
3593 return DW_DLV_OK;
3594 }
3595
3596 /* No errors possible. */
3597 void
3598 dwarf_reset_section_bytes(Dwarf_P_Debug dbg)
3599 {
3600 dbg->de_debug_sects = dbg->de_first_debug_sect;
3601 /* No need to reset; commented out decrement.
3602 dbg->de_n_debug_sect = ???; */
3603 dbg->de_reloc_next_to_return = 0;
3604 dbg->de_sect_sa_next_to_return = 0;
3605 }
3606
3607 /* Storage handler. Gets either a new chunk of memory, or
3608 a pointer in existing memory, from the linked list attached
3609 to dbg at de_debug_sects, depending on size of nbytes
3610
3611 Assume dbg not null, checked in top level routine
3612
3613 Returns a pointer to the allocated buffer space for the
3614 lib to fill in, predincrements next-to-use count so the
3615 space requested is already counted 'used'
3616 when this returns (ie, reserved).
3617
3618 */
3619 Dwarf_Small *
3620 _dwarf_pro_buffer(Dwarf_P_Debug dbg,
3621 int elfsectno, unsigned long nbytes)
3622 {
3623 Dwarf_P_Section_Data cursect = 0;
3624
3625 cursect = dbg->de_current_active_section;
3626 /* By using MAGIC_SECT_NO we allow the
3627 following MAGIC_SECT_NO must
3628 not match any legit section number. test to have just two
3629 clauses (no NULL pointer test) See dwarf_producer_init(). */
3630 if ((cursect->ds_elf_sect_no != elfsectno) ||
3631 ((cursect->ds_nbytes + nbytes) > cursect->ds_orig_alloc)
3632 ) {
3633
3634 /* Either the elf section has changed or there is not enough
3635 space in the current section.
3636
3637 Create a new Dwarf_P_Section_Data_s for the chunk.
3638 and have
3639 space 'on the end' for the buffer itself so we
3640 just do one malloc (not two). */
3641 unsigned long space = nbytes;
3642
3643 if (nbytes < CHUNK_SIZE) {
3644 space = CHUNK_SIZE;
3645 }
3646 cursect = (Dwarf_P_Section_Data)
3647 _dwarf_p_get_alloc(dbg,
3648 sizeof(struct Dwarf_P_Section_Data_s)
3649 + space);
3650 if (cursect == NULL) {
3651 return (NULL);
3652 }
3653 /* _dwarf_p_get_alloc zeroes the space... */
3654
3655 cursect->ds_data = (char *) cursect +
3656 sizeof(struct Dwarf_P_Section_Data_s);
3657 cursect->ds_orig_alloc = space;
3658 cursect->ds_elf_sect_no = elfsectno;
3659 cursect->ds_nbytes = nbytes;/* reserve this number of bytes
3660 of space for caller to fill in */
3661 /* Now link on the end of the list, and mark this one as the
3662 current one */
3663
3664 if (dbg->de_debug_sects->ds_elf_sect_no == MAGIC_SECT_NO) {
3665 /* The only entry is the special one for 'no entry' so
3666 delete that phony one while adding this initial real
3667 one. */
3668 dbg->de_debug_sects = cursect;
3669 dbg->de_current_active_section = cursect;
3670 dbg->de_first_debug_sect = cursect;
3671 } else {
3672 dbg->de_current_active_section->ds_next = cursect;
3673 dbg->de_current_active_section = cursect;
3674 }
3675 dbg->de_n_debug_sect++;
3676
3677 return ((Dwarf_Small *) cursect->ds_data);
3678 }
3679
3680 /* There is enough space in the current buffer */
3681 {
3682 Dwarf_Small *space_for_caller = (Dwarf_Small *)
3683 (cursect->ds_data + cursect->ds_nbytes);
3684
3685 cursect->ds_nbytes += nbytes;
3686 return space_for_caller;
3687 }
3688 }