Debian Codegraph @

src/lib/libdwarfp/dwarf_pro_dnames.c

raw
1 /*
2 Copyright 2018-2018 David Anderson. All Rights Reserved.
3
4 This program is free software; you can redistribute it
5 and/or modify it under the terms of version 2.1 of the
6 GNU Lesser General Public License as published by the Free
7 Software Foundation.
8
9 This program is distributed in the hope that it would be
10 useful, but WITHOUT ANY WARRANTY; without even the implied
11 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE.
13
14 Further, this software is distributed without any warranty
15 that it is free of the rightful claim of any third person
16 regarding infringement or the like. Any license provided
17 herein, whether implied or otherwise, applies only to this
18 software file. Patent licenses, if any, provided herein
19 do not apply to combinations of this program with other
20 software, or any other product whatsoever.
21
22 You should have received a copy of the GNU Lesser General
23 Public License along with this program; if not, write the
24 Free Software Foundation, Inc., 51 Franklin Street - Fifth
25 Floor, Boston MA 02110-1301, USA.
26
27 */
28
29 #include <config.h>
30
31 #include <stddef.h> /* NULL */
32 #include <stdio.h> /* printf() */
33 #include <string.h> /* memcpy() */
34
35 #ifdef HAVE_STDINT_H
36 #include <stdint.h> /* uintptr_t */
37 #endif
38
39 #include "dwarf.h"
40 #include "libdwarf.h"
41 #include "dwarf_base_types.h"
42 #include "libdwarfp.h"
43 #include "dwarf_pro_incl.h"
44 #include "dwarf_pro_opaque.h"
45 #include "dwarf_pro_error.h"
46 #include "dwarf_pro_alloc.h"
47 #include "dwarf_pro_arange.h"
48 #include "dwarf_pro_section.h"
49 #include "dwarf_pro_reloc.h"
50 #include "dwarf_pro_dnames.h"
51
52 static struct Dwarf_P_Dnames_Head_s staticdnames =
53 {
54 /* fake unit length*/58,
55 /* version */5,
56 /*offset size*/4,
57 /*offset*/0,
58 /* cu count */1,
59 /* tu ct */0,
60 /* foreigntu ct */0,
61 /* bucket ct */0,
62 /* name ct */1,
63 /* abbrev_table_size*/7,
64 /* augstringsize*/0,
65 /* augstring*/0
66 };
67
68 /* total length encoded: 6 bytes. */
69 unsigned char abbrv[7] =
70 {/* abbrev code*/ 2,
71 /* DW_TAG_subprogram */ 0x2e,
72 /* DW_IDX_compile_unit*/1,
73 /* DW_FORM_udata */7,
74 /* end abbrev */ 0,0,
75 /* end abbrev block */0};
76
77 unsigned char entry[3] =
78 { /* abbrev code */2,
79 /* Offset of subprogram */0x32,
80 /* terminate this abbrev set */0 };
81
82 Dwarf_Unsigned stroffsets[1] = {0x42}; /* main */
83
84 /* Set to match subprog */
85 Dwarf_Unsigned dieoffset[1] = {12};
86
87 int
88 dwarf_force_dnames(Dwarf_P_Debug dbg,
89 int elfsectno,
90 Dwarf_Error * error)
91 {
92 Dwarf_P_Dnames dn;
93 Dwarf_Unsigned datalen = 0;
94 Dwarf_Unsigned withunitlength = 0;
95 unsigned char *data = 0;
96 unsigned char *startdata = 0;
97 unsigned int zero = 0;
98 struct Dwarf_P_Dnames_Head_s *dh = &staticdnames;
99 uintptr_t bytes = 0;
100
101 if (dbg == NULL) {
102 _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
103 return DW_DLV_ERROR;
104 }
105 if (!elfsectno) {
106 dbg->de_force_dnames = TRUE;
107 dn = (Dwarf_P_Dnames)
108 _dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_P_Dnames_s));
109 if (dn == NULL) {
110 _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
111 return DW_DLV_ERROR;
112 }
113 dbg->de_dnames = dn;
114 return DW_DLV_OK;
115 }
116 dn = dbg->de_dnames;
117 if (!dbg->de_dnames) {
118 return DW_DLV_NO_ENTRY;
119 }
120 dn->dn_dbg = dbg;
121 dn->dn_create_section = TRUE;
122 datalen = 8*4 +
123 /* offset CU */
124 dh->dh_offset_size +
125 /* str offsets, entry offsets */
126 dh->dh_offset_size *2 +
127 sizeof(abbrv) +
128 sizeof(entry);
129 withunitlength = datalen + 4;/* 4 byte length, 32 bit offset */
130
131 GET_CHUNK(dbg, dbg->de_elf_sects[DEBUG_NAMES],
132 data, (unsigned long)withunitlength, error);
133 startdata = data;
134
135 /*WRITE_UNALIGNED(dbg,dest,source, srclength,len_out)*/
136 /* 1 */
137 WRITE_UNALIGNED(dbg, (void *)data,
138 (const void *)&datalen,
139 sizeof(datalen) , SIZEOFT32);
140 data += SIZEOFT32;
141 /* 2 */
142 WRITE_UNALIGNED(dbg, (void *)data,
143 (const void *)&dh->dh_version,
144 sizeof(dh->dh_version),
145 SIZEOFT16);
146 data += SIZEOFT16;
147
148 /* 3 */
149 WRITE_UNALIGNED(dbg, (void *)data,
150 (const void *)&zero,sizeof(zero),
151 SIZEOFT16);
152 data += SIZEOFT16;
153
154 /* 4 */
155 WRITE_UNALIGNED(dbg, (void *)data,
156 (const void *)&dh->dh_comp_unit_count,
157 sizeof(dh->dh_comp_unit_count),
158 SIZEOFT32);
159 data += SIZEOFT32;
160 /* 5 */
161 WRITE_UNALIGNED(dbg, (void *)data,
162 (const void *)&dh->dh_local_type_unit_count,
163 sizeof(dh->dh_local_type_unit_count),
164 SIZEOFT32);
165 data += SIZEOFT32;
166 /* 6 */
167
168 WRITE_UNALIGNED(dbg, (void *)data,
169 (const void *)&dh->dh_foreign_type_unit_count,
170 sizeof(dh->dh_foreign_type_unit_count),
171 SIZEOFT32);
172 data += SIZEOFT32;
173 /* 7 */
174 WRITE_UNALIGNED(dbg, (void *)data,
175 (const void *)&dh->dh_bucket_count,
176 sizeof(dh->dh_bucket_count),
177 SIZEOFT32);
178 data += SIZEOFT32;
179 /* 8 */
180 WRITE_UNALIGNED(dbg, (void *)data,
181 (const void *)&dh->dh_name_count,
182 sizeof(dh->dh_name_count),
183 SIZEOFT32);
184 data += SIZEOFT32;
185
186 /* 9 */
187 WRITE_UNALIGNED(dbg, (void *)data,
188 (const void *)&dh->dh_abbrev_table_size,
189 sizeof(dh->dh_abbrev_table_size),
190 SIZEOFT32);
191 data += SIZEOFT32;
192
193 /* 10 */
194 WRITE_UNALIGNED(dbg, (void *)data,
195 (const void *)&dh->dh_augmentation_string_size,
196 sizeof(dh->dh_augmentation_string_size),
197 SIZEOFT32);
198 data += SIZEOFT32;
199 if (dh->dh_augmentation_string_size) {
200 /* 11 */
201 memcpy((void *)data,dh->dh_augmentation_string,
202 dh->dh_augmentation_string_size);
203 data += dh->dh_augmentation_string_size;
204 bytes = data - startdata;
205 }
206 bytes = data - startdata;
207
208 /* The CU offset table. A single entry */
209 WRITE_UNALIGNED(dbg, (void *)data,
210 (const void *)&zero,
211 sizeof(stroffsets[0]),
212 dh->dh_offset_size);
213 data += dh->dh_offset_size;
214 bytes = data - startdata;
215
216 /* Now the string offsets table */
217 WRITE_UNALIGNED(dbg, (void *)data,
218 (const void *)&stroffsets[0],
219 sizeof(stroffsets[0]),
220 dh->dh_offset_size);
221 data += dh->dh_offset_size;
222 bytes = data - startdata;
223 /* Now the Entry Offsets (DIE offsets) array */
224 WRITE_UNALIGNED(dbg, (void *)data,
225 (const void *)&dieoffset[0],
226 sizeof(dieoffset[0]),
227 dh->dh_offset_size);
228 data += dh->dh_offset_size;
229 bytes = data - startdata;
230
231 memcpy((void *)data,abbrv,sizeof(abbrv));
232 data += sizeof(abbrv);
233 bytes = data - startdata;
234
235 memcpy((void *)data,entry,sizeof(entry));
236 data += sizeof(entry);
237 bytes = data - startdata;
238 if (bytes != withunitlength) {
239 printf("FAIL writing debug_names "
240 "bytes written: %lu "
241 "bytes allocated: %lu\n",
242 (unsigned long)bytes,
243 (unsigned long)withunitlength);
244 }
245 #if 0
246 dbg->de_dnames_blob = startdata;
247 dbg->de_dnames_bloblength = withunitlength;
248 #endif
249 return DW_DLV_OK;
250 }