Debian Codegraph @

src/bin/dwarfexample/jitreader.c

raw
1 /* Copyright (c) 2021 David Anderson
2 This test code is hereby placed in the public domain
3 for anyone to use in any way. */
4
5 /*! @file jitreader.c
6 @defgroup jitreader Demonstrating reading DWARF without a file.
7 @brief How to read DWARF2 and later from memory.
8
9 @code
10 */
11
12 #include <config.h>
13
14 #include <stddef.h> /* NULL */
15 #include <stdio.h> /* printf() */
16 #include <stdlib.h> /* exit() */
17 #include <string.h> /* strcmp() */
18
19 #include "dwarf.h"
20 #include "libdwarf.h"
21 #include "libdwarf_private.h"
22
23 /*
24 This demonstates processing DWARF
25 from in_memory data. For simplicity
26 in this example we are using static arrays.
27 The C source is src/bin/dwarfexample/jitreader.c
28
29 The motivation is from JIT compiling, where
30 at runtime of some application, it generates
31 code on the file and DWARF information for it too.
32
33 This gives an example of enabling all of libdwarf's
34 functions without actually having the DWARF information
35 in a file. (If you have a file in some odd format
36 you can use this approach to have libdwarf access
37 the format for DWARF data and work normally without
38 ever exposing the format to libdwarf.)
39
40 None of the structures defined here in this source
41 (or any source using this feature)
42 are ever known to libdwarf. They are totally
43 private to your code.
44 The code you write (like this example) you compile
45 separate from libdwarf. You never place your code
46 into libdwarf, you just link your code into
47 your application and link against libdwarf.
48 */
49
50 /* Some valid DWARF2 data */
51 static Dwarf_Small abbrevbytes[] = {
52 0x01, 0x11, 0x01, 0x25, 0x0e, 0x13, 0x0b, 0x03, 0x08, 0x1b,
53 0x0e, 0x11, 0x01, 0x12, 0x01, 0x10, 0x06, 0x00, 0x00, 0x02,
54 0x2e, 0x01, 0x3f, 0x0c, 0x03, 0x08, 0x3a, 0x0b, 0x3b, 0x0b,
55 0x39, 0x0b, 0x27, 0x0c, 0x11, 0x01, 0x12, 0x01, 0x40, 0x06,
56 0x97, 0x42, 0x0c, 0x01, 0x13, 0x00, 0x00, 0x03, 0x34, 0x00,
57 0x03, 0x08, 0x3a, 0x0b, 0x3b, 0x0b, 0x39, 0x0b, 0x49, 0x13,
58 0x02, 0x0a, 0x00, 0x00, 0x04, 0x24, 0x00, 0x0b, 0x0b, 0x3e,
59 0x0b, 0x03, 0x08, 0x00, 0x00, 0x00, };
60 static Dwarf_Small infobytes[] = {
61 0x60, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
62 0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x74, 0x2e, 0x63,
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x66, 0x00, 0x01,
66 0x02, 0x06, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0x00, 0x01, 0x5c, 0x00, 0x00, 0x00, 0x03, 0x69,
69 0x00, 0x01, 0x03, 0x08, 0x5c, 0x00, 0x00, 0x00, 0x02, 0x91,
70 0x6c, 0x00, 0x04, 0x04, 0x05, 0x69, 0x6e, 0x74, 0x00, 0x00, };
71 static Dwarf_Small strbytes[] = {
72 0x47, 0x4e, 0x55, 0x20, 0x43, 0x31, 0x37, 0x20, 0x39, 0x2e,
73 0x33, 0x2e, 0x30, 0x20, 0x2d, 0x6d, 0x74, 0x75, 0x6e, 0x65,
74 0x3d, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x69, 0x63, 0x20, 0x2d,
75 0x6d, 0x61, 0x72, 0x63, 0x68, 0x3d, 0x78, 0x38, 0x36, 0x2d,
76 0x36, 0x34, 0x20, 0x2d, 0x67, 0x64, 0x77, 0x61, 0x72, 0x66,
77 0x2d, 0x32, 0x20, 0x2d, 0x4f, 0x30, 0x20, 0x2d, 0x66, 0x61,
78 0x73, 0x79, 0x6e, 0x63, 0x68, 0x72, 0x6f, 0x6e, 0x6f, 0x75,
79 0x73, 0x2d, 0x75, 0x6e, 0x77, 0x69, 0x6e, 0x64, 0x2d, 0x74,
80 0x61, 0x62, 0x6c, 0x65, 0x73, 0x20, 0x2d, 0x66, 0x73, 0x74,
81 0x61, 0x63, 0x6b, 0x2d, 0x70, 0x72, 0x6f, 0x74, 0x65, 0x63,
82 0x74, 0x6f, 0x72, 0x2d, 0x73, 0x74, 0x72, 0x6f, 0x6e, 0x67,
83 0x20, 0x2d, 0x66, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x2d, 0x63,
84 0x6c, 0x61, 0x73, 0x68, 0x2d, 0x70, 0x72, 0x6f, 0x74, 0x65,
85 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x2d, 0x66, 0x63, 0x66,
86 0x2d, 0x70, 0x72, 0x6f, 0x74, 0x65, 0x63, 0x74, 0x69, 0x6f,
87 0x6e, 0x00, 0x2f, 0x76, 0x61, 0x72, 0x2f, 0x74, 0x6d, 0x70,
88 0x2f, 0x74, 0x69, 0x6e, 0x79, 0x64, 0x77, 0x61, 0x72, 0x66,
89 0x00, };
90
91 /* An internals_t , data used elsewhere but
92 not directly visible elsewhere. One needs to have one
93 of these but maybe the content here too little
94 or useless, this is just an example of sorts. */
95 #define SECCOUNT 4
96 struct sectiondata_s {
97 unsigned int sd_addr;
98 unsigned int sd_objoffsetlen;
99 unsigned int sd_objpointersize;
100 Dwarf_Unsigned sd_sectionsize;
101 const char * sd_secname;
102 Dwarf_Small * sd_content;
103 };
104
105 /* The secname must not be 0 , pass "" if
106 there is no name. */
107 static struct sectiondata_s sectiondata[SECCOUNT] = {
108 {0,0,0,0,"",0},
109 {0,32,32,sizeof(abbrevbytes),".debug_abbrev",abbrevbytes},
110 {0,32,32,sizeof(infobytes),".debug_info",infobytes},
111 {0,32,32,sizeof(strbytes),".debug_str",strbytes}
112 };
113
114 typedef struct special_filedata_s {
115 int f_is_64bit;
116 Dwarf_Small f_object_endian;
117 unsigned f_pointersize;
118 unsigned f_offsetsize;
119 Dwarf_Unsigned f_filesize;
120 Dwarf_Unsigned f_sectioncount;
121 struct sectiondata_s * f_sectarray;
122 } special_filedata_internals_t;
123
124 /* Use DW_END_little.
125 Libdwarf finally sets the file-format-specific
126 f_object_endianness field to a DW_END_little or
127 DW_END_big (see dwarf.h).
128 Here we must do that ourselves. */
129 static special_filedata_internals_t base_internals =
130 { FALSE,DW_END_little,32,32,200,SECCOUNT, sectiondata };
131
132 static
133 int gsinfo(void* obj,
134 Dwarf_Unsigned section_index,
135 Dwarf_Obj_Access_Section_a* return_section,
136 int* error )
137 {
138 special_filedata_internals_t *internals =
139 (special_filedata_internals_t *)(obj);
140 struct sectiondata_s *finfo = 0;
141
142 *error = 0; /* No error. Avoids unused arg */
143 if (section_index >= SECCOUNT) {
144 return DW_DLV_NO_ENTRY;
145 }
146 finfo = internals->f_sectarray + section_index;
147 return_section->as_name = finfo->sd_secname;
148 return_section->as_type = 0;
149 return_section->as_flags = 0;
150 return_section->as_addr = finfo->sd_addr;
151 return_section->as_offset = 0;
152 return_section->as_size = finfo->sd_sectionsize;
153 return_section->as_link = 0;
154 return_section->as_info = 0;
155 return_section->as_addralign = 0;
156 return_section->as_entrysize = 1;
157 return DW_DLV_OK;
158 }
159 static Dwarf_Small
160 gborder(void * obj)
161 {
162 special_filedata_internals_t *internals =
163 (special_filedata_internals_t *)(obj);
164 return internals->f_object_endian;
165 }
166 static
167 Dwarf_Small glensize(void * obj)
168 {
169 /* offset size */
170 special_filedata_internals_t *internals =
171 (special_filedata_internals_t *)(obj);
172 return internals->f_offsetsize/8;
173 }
174 static
175 Dwarf_Small gptrsize(void * obj)
176 {
177 special_filedata_internals_t *internals =
178 (special_filedata_internals_t *)(obj);
179 return internals->f_pointersize/8;
180 }
181 static
182 Dwarf_Unsigned gfilesize(void * obj)
183 {
184 special_filedata_internals_t *internals =
185 (special_filedata_internals_t *)(obj);
186 return internals->f_filesize;
187 }
188 static
189 Dwarf_Unsigned gseccount(void* obj)
190 {
191 special_filedata_internals_t *internals =
192 (special_filedata_internals_t *)(obj);
193 return internals->f_sectioncount;
194 }
195 static
196 int gloadsec(void * obj,
197 Dwarf_Unsigned secindex,
198 Dwarf_Small**rdata,
199 int *error)
200 {
201 special_filedata_internals_t *internals =
202 (special_filedata_internals_t *)(obj);
203 struct sectiondata_s *secp = 0;
204
205 *error = 0; /* No Error, avoids compiler warning */
206 if (secindex >= internals->f_sectioncount) {
207 return DW_DLV_NO_ENTRY;
208 }
209 secp = secindex +internals->f_sectarray;
210 *rdata = secp->sd_content;
211 return DW_DLV_OK;
212 }
213
214 const Dwarf_Obj_Access_Methods_a methods = {
215 gsinfo,
216 gborder,
217 glensize,
218 gptrsize,
219 gfilesize,
220 gseccount,
221 gloadsec,
222 0 /* no relocating anything */
223 };
224 struct Dwarf_Obj_Access_Interface_a_s dw_interface =
225 { &base_internals,&methods };
226
227 static const Dwarf_Sig8 zerosignature;
228 static int
229 isformstring(Dwarf_Half form)
230 {
231 /* Not handling every form string, just the
232 ones used in simple cases. */
233 switch(form) {
234 case DW_FORM_string: return TRUE;
235 case DW_FORM_GNU_strp_alt: return TRUE;
236 case DW_FORM_GNU_str_index: return TRUE;
237 case DW_FORM_strx: return TRUE;
238 case DW_FORM_strx1: return TRUE;
239 case DW_FORM_strx2: return TRUE;
240 case DW_FORM_strx3: return TRUE;
241 case DW_FORM_strx4: return TRUE;
242 case DW_FORM_strp: return TRUE;
243 default: break;
244 };
245 return FALSE;
246 }
247
248 static int
249 print_attr(Dwarf_Attribute atr,
250 Dwarf_Signed anumber, Dwarf_Error *error)
251 {
252 int res = 0;
253 char *str = 0;
254 const char *attrname = 0;
255 const char *formname = 0;
256 Dwarf_Half form = 0;
257 Dwarf_Half attrnum = 0;
258 res = dwarf_whatform(atr,&form,error);
259 if (res != DW_DLV_OK) {
260 printf("dwarf_whatform failed! res %d\n",res);
261 return res;
262 }
263 res = dwarf_whatattr(atr,&attrnum,error);
264 if (res != DW_DLV_OK) {
265 printf("dwarf_whatattr failed! res %d\n",res);
266 return res;
267 }
268 res = dwarf_get_AT_name(attrnum,&attrname);
269 if (res == DW_DLV_NO_ENTRY) {
270 printf("Bogus attrnum 0x%x\n",attrnum);
271 attrname = "<internal error ?>";
272 }
273 res = dwarf_get_FORM_name(form,&formname);
274 if (res == DW_DLV_NO_ENTRY) {
275 printf("Bogus form 0x%x\n",attrnum);
276 attrname = "<internal error ?>";
277 }
278 if (!isformstring(form)) {
279 printf(" [%2d] Attr: %-15s Form: %-15s\n",
280 (int)anumber,attrname,formname);
281 return DW_DLV_OK;
282 }
283 res = dwarf_formstring(atr,&str,error);
284 if (res != DW_DLV_OK) {
285 printf("dwarf_formstring failed! res %d\n",res);
286 return res;
287 }
288 printf(" [%2d] Attr: %-15s Form: %-15s %s\n",
289 (int)anumber,attrname,formname,str);
290 return DW_DLV_OK;
291 }
292
293 static void
294 dealloc_list(Dwarf_Debug dbg,
295 Dwarf_Attribute *attrbuf,
296 Dwarf_Signed attrcount,
297 Dwarf_Signed i)
298 {
299 for ( ; i < attrcount; ++i) {
300 dwarf_dealloc_attribute(attrbuf[i]);
301 }
302 dwarf_dealloc(dbg,attrbuf,DW_DLA_LIST);
303 }
304
305 static int
306 print_one_die(Dwarf_Debug dbg,Dwarf_Die in_die,int level,
307 Dwarf_Error *error)
308 {
309 Dwarf_Attribute *attrbuf = 0;
310 Dwarf_Signed attrcount = 0;
311 Dwarf_Half tag = 0;
312 const char * tagname = 0;
313 int res = 0;
314 Dwarf_Signed i = 0;
315
316 res = dwarf_tag(in_die,&tag,error);
317 if (res != DW_DLV_OK) {
318 printf("dwarf_tag failed! res %d\n",res);
319 return res;
320 }
321 res = dwarf_get_TAG_name(tag,&tagname);
322 if (res != DW_DLV_OK) {
323 tagname = "<bogus tag>";
324 }
325 printf("%3d: Die: %s\n",level,tagname);
326 res = dwarf_attrlist(in_die,&attrbuf,&attrcount,error);
327 if (res != DW_DLV_OK) {
328 printf("dwarf_attrlist failed! res %d\n",res);
329 return res;
330 }
331 for (i = 0; i <attrcount;++i) {
332 res =print_attr(attrbuf[i],i,error);
333 if (res != DW_DLV_OK) {
334 dealloc_list(dbg,attrbuf,attrcount,0);
335 printf("dwarf_attr print failed! res %d\n",res);
336 return res;
337 }
338 }
339 dealloc_list(dbg,attrbuf,attrcount,0);
340 return DW_DLV_OK;
341 }
342
343 static int
344 print_object_info(Dwarf_Debug dbg,Dwarf_Error *error)
345 {
346 Dwarf_Bool is_info = TRUE; /* our data is not DWARF4
347 .debug_types. */
348 Dwarf_Unsigned cu_header_length = 0;
349 Dwarf_Half version_stamp = 0;
350 Dwarf_Off abbrev_offset = 0;
351 Dwarf_Half address_size = 0;
352 Dwarf_Half length_size = 0;
353 Dwarf_Half extension_size = 0;
354 Dwarf_Sig8 type_signature;
355 Dwarf_Unsigned typeoffset = 0;
356 Dwarf_Unsigned next_cu_header_offset = 0;
357 Dwarf_Half header_cu_type = 0;
358 int res = 0;
359 Dwarf_Die cu_die = 0;
360 int level = 0;
361
362 type_signature = zerosignature;
363 res = dwarf_next_cu_header_d(dbg,
364 is_info,
365 &cu_header_length,
366 &version_stamp,
367 &abbrev_offset,
368 &address_size,
369 &length_size,
370 &extension_size,
371 &type_signature,
372 &typeoffset,
373 &next_cu_header_offset,
374 &header_cu_type,
375 error);
376 if (res != DW_DLV_OK) {
377 printf("Next cu header result %d. "
378 "Something is wrong FAIL, line %d\n",res,__LINE__);
379 if (res == DW_DLV_ERROR) {
380 printf("Error is: %s\n",dwarf_errmsg(*error));
381 }
382 exit(EXIT_FAILURE);
383 }
384 printf("CU header length..........0x%lx\n",
385 (unsigned long)cu_header_length);
386 printf("Version stamp.............%d\n",version_stamp);
387 printf("Address size .............%d\n",address_size);
388 printf("Offset size...............%d\n",length_size);
389 printf("Next cu header offset.....0x%lx\n",
390 (unsigned long)next_cu_header_offset);
391
392 res = dwarf_siblingof_b(dbg, NULL,is_info, &cu_die, error);
393 if (res != DW_DLV_OK) {
394 /* There is no CU die, which should be impossible. */
395 if (res == DW_DLV_ERROR) {
396 printf("ERROR: dwarf_siblingof_b failed, no CU die\n");
397 printf("Error is: %s\n",dwarf_errmsg(*error));
398 return res;
399 } else {
400 printf("ERROR: dwarf_siblingof_b got NO_ENTRY, "
401 "no CU die\n");
402 return res;
403 }
404 }
405 res = print_one_die(dbg,cu_die,level,error);
406 if (res != DW_DLV_OK) {
407 dwarf_dealloc_die(cu_die);
408 printf("print_one_die failed! %d\n",res);
409 return res;
410 }
411 dwarf_dealloc_die(cu_die);
412 return DW_DLV_OK;
413 }
414
415 /* testing interfaces useful for embedding
416 libdwarf inside another program or library. */
417 int main(int argc, char **argv)
418 {
419 int res = 0;
420 Dwarf_Debug dbg = 0;
421 Dwarf_Error error = 0;
422 int fail = FALSE;
423 int i = 1;
424
425 if (i >= argc) {
426 /* OK */
427 } else {
428 if (!strcmp(argv[i],"--suppress-de-alloc-tree")) {
429 /* Do nothing, ignore the argument */
430 ++i;
431 }
432 }
433 /* Fill in interface before this call.
434 We are using a static area, see above. */
435 res = dwarf_object_init_b(&dw_interface,
436 0,0,DW_GROUPNUMBER_ANY,&dbg,
437 &error);
438 if (res == DW_DLV_NO_ENTRY) {
439 printf("FAIL Cannot dwarf_object_init_b() NO ENTRY. \n");
440 exit(EXIT_FAILURE);
441 } else if (res == DW_DLV_ERROR){
442 printf("FAIL Cannot dwarf_object_init_b(). \n");
443 printf("msg: %s\n",dwarf_errmsg(error));
444 dwarf_dealloc_error(dbg,error);
445 exit(EXIT_FAILURE);
446 }
447 res = print_object_info(dbg,&error);
448 if (res != DW_DLV_OK) {
449 if (res == DW_DLV_ERROR) {
450 dwarf_dealloc_error(dbg,error);
451 }
452 printf("FAIL printing, res %d line %d\n",res,__LINE__);
453 exit(EXIT_FAILURE);
454 }
455 dwarf_object_finish(dbg);
456 if (fail) {
457 printf("FAIL objectaccess.c\n");
458 exit(EXIT_FAILURE);
459 }
460 return 0;
461 }
462 /*! @endcode */