Debian Codegraph @

src/bin/dwarfdump/print_str_offsets.c

raw
1 /*
2 Copyright 2018-2019 David Anderson. All rights reserved.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of version 2 of the GNU General
6 Public License as published by the Free Software Foundation.
7
8 This program is distributed in the hope that it would be
9 useful, but WITHOUT ANY WARRANTY; without even the implied
10 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE.
12
13 Further, this software is distributed without any warranty
14 that it is free of the rightful claim of any third person
15 regarding infringement or the like. Any license provided
16 herein, whether implied or otherwise, applies only to this
17 software file. Patent licenses, if any, provided herein
18 do not apply to combinations of this program with other
19 software, or any other product whatsoever.
20
21 You should have received a copy of the GNU General Public
22 License along with this program; if not, write the Free
23 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
24 Boston MA 02110-1301, USA.
25 */
26
27 #include <config.h>
28 #include <stdio.h> /* FILE decl for dd_esb.h, printf etc */
29
30 #include "dwarf.h"
31 #include "libdwarf.h"
32 #include "libdwarf_private.h"
33 #include "dd_defined_types.h"
34 #include "dd_checkutil.h"
35 #include "dd_glflags.h"
36 #include "dd_globals.h"
37 #include "dd_naming.h"
38 #include "dd_esb.h"
39 #include "dd_esb_using_functions.h"
40 #include "dd_sanitized.h"
41
42 /* print data in .debug_str_offsets.
43 There is no guarantee this will work because
44 the DWARF5 standard is silent about
45 whether arbitrary non-zero bytes, or odd
46 alignments, or unused data spaces are allowed
47 in the section
48 */
49 int
50 print_str_offsets_section(Dwarf_Debug dbg,Dwarf_Error *err)
51 {
52 int res = 0;
53 Dwarf_Str_Offsets_Table sot = 0;
54 Dwarf_Unsigned wasted_byte_count = 0;
55 Dwarf_Unsigned table_count = 0;
56 Dwarf_Unsigned tabnum = 0;
57
58 res = dwarf_open_str_offsets_table_access(dbg, &sot,err);
59 if (res == DW_DLV_NO_ENTRY) {
60 /* No such table */
61 return res;
62 }
63 if (res == DW_DLV_ERROR) {
64 return res;
65 }
66 for (;; ++tabnum) {
67 Dwarf_Unsigned unit_length =0;
68 Dwarf_Unsigned unit_length_offset =0;
69 Dwarf_Unsigned table_start_offset =0;
70 Dwarf_Half entry_size = 0;
71 Dwarf_Half version =0;
72 Dwarf_Half padding =0;
73 Dwarf_Unsigned table_value_count =0;
74 Dwarf_Unsigned i = 0;
75 Dwarf_Unsigned table_entry_value = 0;
76 unsigned rowlim = 4;
77 unsigned count_in_row = 0;
78
79 res = dwarf_next_str_offsets_table(sot,
80 &unit_length, &unit_length_offset,
81 &table_start_offset,
82 &entry_size,&version,&padding,
83 &table_value_count,err);
84 if (res == DW_DLV_NO_ENTRY) {
85 /* We have dealt with all tables */
86 break;
87 }
88 if (res == DW_DLV_ERROR) {
89 dwarf_close_str_offsets_table_access(sot,err);
90 return res;
91 }
92 if (tabnum == 0) {
93 struct esb_s truename;
94 char buf[DWARF_SECNAME_BUFFER_SIZE];
95
96 esb_constructor_fixed(&truename,buf,sizeof(buf));
97 get_true_section_name(dbg,".debug_str_offsets",
98 &truename,TRUE);
99 printf("\n%s\n",sanitized(esb_get_string(&truename)));
100 esb_destructor(&truename);
101 } else {
102 printf("\n");
103 }
104 printf(" table %" DW_PR_DUu "\n",tabnum);
105 printf(" tableheader 0x%" DW_PR_XZEROS DW_PR_DUx "\n",
106 unit_length_offset);
107 printf(" arrayoffset 0x%" DW_PR_XZEROS DW_PR_DUx "\n",
108 table_start_offset);
109 printf(" unit length 0x%" DW_PR_XZEROS DW_PR_DUx "\n",
110 unit_length);
111 printf(" entry size %u\n",entry_size);
112 printf(" version %u\n",version);
113 if (padding) {
114 printf("Error: padding is non-zero. "
115 "Something is wrong.\n");
116 }
117 printf(" padding 0x%x\n",padding);
118 printf(" arraysize %" DW_PR_DUu "\n",table_value_count);
119
120 /* Lets print 4 per row. */
121 count_in_row = 0;
122 for (i=0; i < table_value_count; ++i) {
123
124 res = dwarf_str_offsets_value_by_index(sot,i,
125 &table_entry_value,err);
126 if (res != DW_DLV_OK) {
127 dwarf_close_str_offsets_table_access(sot,err);
128 return res;
129 }
130 if (!count_in_row) {
131 printf(" Entry [%4" DW_PR_DUu "]: ",i);
132 }
133 printf(" 0x%" DW_PR_XZEROS DW_PR_DUx ,
134 table_entry_value);
135 ++count_in_row;
136 if (count_in_row < rowlim) {
137 continue;
138 }
139 printf("\n");
140 count_in_row = 0;
141 }
142 if (count_in_row) {
143 printf("\n");
144 }
145 res = dwarf_str_offsets_statistics(sot,&wasted_byte_count,
146 &table_count,err);
147 if (res == DW_DLV_OK) {
148 printf(" wasted %" DW_PR_DUu " bytes\n",
149 wasted_byte_count);
150 }
151 if (res == DW_DLV_ERROR) {
152 res = dwarf_close_str_offsets_table_access(sot,err);
153 return res;
154 }
155 }
156 if (wasted_byte_count) {
157 res = dwarf_str_offsets_statistics(sot,&wasted_byte_count,
158 &table_count,err);
159 if (res == DW_DLV_OK) {
160 printf(" finalwasted %" DW_PR_DUu " bytes\n",
161 wasted_byte_count);
162 } else {
163 res = dwarf_close_str_offsets_table_access(sot,err);
164 return res;
165 }
166 }
167 res = dwarf_close_str_offsets_table_access(sot,err);
168 sot = 0;
169 return res;
170 }