summaryrefslogtreecommitdiffstats
path: root/dwarf.c
blob: 51dbe6c80b2129222fe1cc68c70439e945e0ac93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <strings.h>
#include <errno.h>
#include "dwarf.h"
#include "libdwarf.h"


static void parse_dwarf(Dwarf_Debug dbg, const char *struct_name, const char *field_names[], int field_count);
static void find_struct(Dwarf_Debug dbg, Dwarf_Die die, const char *struct_name, const char *field_names[], int field_count, int level);
static void find_fields(Dwarf_Debug dbg, Dwarf_Die die, const char *struct_name, const char *field_names[], int field_count, int level);
static void print_field(Dwarf_Debug dbg, Dwarf_Die die, const char *field_name, int pad_num);

int debug = 0;

void usage(char *argv[]) {
	printf("%s debug_file struct_name field [field...]\n", argv[0]);
}

int main(int argc, char *argv[]) {
	Dwarf_Debug dbg = 0;
	int fd = -1;
	const char *filepath;
	const char *struct_name, **field_names;
	int res = DW_DLV_ERROR;
	Dwarf_Error error;
	Dwarf_Handler errhand = 0;
	Dwarf_Ptr errarg = 0;

	if(argc < 4) {
		usage(argv);
		exit(1);
	}

	filepath = argv[1];
	struct_name = argv[2];
	field_names = argv + 3;

	fd = open(filepath,O_RDONLY);
	if(fd < 0) {
		printf("Failure attempting to open %s\n",filepath);
	}
	res = dwarf_init(fd,DW_DLC_READ,errhand,errarg, &dbg,&error);
	if(res != DW_DLV_OK) {
		printf("Giving up, cannot do DWARF processing\n");
		exit(1);
	}

	parse_dwarf(dbg, struct_name, field_names, argc - 3);

	res = dwarf_finish(dbg,&error);
	if(res != DW_DLV_OK) {
		printf("dwarf_finish failed!\n");
	}
	close(fd);
	return 0;
}

static void parse_dwarf(Dwarf_Debug dbg, const char *struct_name,
		const char *field_names[], int field_count) {
	Dwarf_Bool is_info = 1;
	Dwarf_Unsigned cu_length;
	Dwarf_Half cu_version;
	Dwarf_Off cu_abbrev_offset;
	Dwarf_Half cu_pointer_size;
	Dwarf_Half cu_offset_size;
	Dwarf_Half cu_extension_size;
	Dwarf_Sig8 type_signature;
	Dwarf_Unsigned type_offset;
	Dwarf_Unsigned cu_next_offset;
	Dwarf_Error err;
	int rc;


	while (1) {
		Dwarf_Die die;

		rc = dwarf_next_cu_header_c(dbg, is_info, &cu_length,
			&cu_version, &cu_abbrev_offset, &cu_pointer_size,
			&cu_offset_size, &cu_extension_size, &type_signature,
			&type_offset, &cu_next_offset, &err);

		if (rc == DW_DLV_NO_ENTRY)
			break;
		if (rc != DW_DLV_OK) {
			printf("error dwarf_next_cu_header_c: %d %s\n", rc, dwarf_errmsg(err));
			exit(1);
		}


		rc = dwarf_siblingof(dbg, NULL, &die, &err);
		if (rc != DW_DLV_OK) {
			printf("first dwarf_siblingof failed: %d %s\n", rc, dwarf_errmsg(err));
			exit(1);
		}

		find_struct(dbg, die, struct_name, field_names, field_count, 0);
	}

	printf("struct %s not found\n", struct_name);
	exit(2);
}

static void find_struct(Dwarf_Debug dbg, Dwarf_Die die, const char *struct_name, const char *field_names[], int field_count, int level) {
	Dwarf_Die next;
	Dwarf_Error err;
	int rc;

	if (level > 1)
		return;

	do {
		char *name;
		const char *tag_name;
		Dwarf_Half tag;

		rc = dwarf_diename(die, &name, &err);
		if (rc == DW_DLV_NO_ENTRY) {
			name = NULL;
		} else if (rc != DW_DLV_OK) {
			printf("dwarf_diename error: %d %s\n", rc, dwarf_errmsg(err));
			exit(1);
		}

		rc = dwarf_tag(die, &tag, &err);
		if (rc != DW_DLV_OK) {
			printf("dwarf_tag error: %d %s\n", rc, dwarf_errmsg(err));
			exit(1);
		}

		if (debug) {
			rc = dwarf_get_TAG_name(tag, &tag_name);
			if (rc != DW_DLV_OK) {
				printf("dwarf_get_TAG_name error: %d\n", rc);
				exit(1);
			}

			printf("<%d> %p <%d> %s: %s\n", level, die, tag, tag_name, name ? name : "<no name>");
		}

		rc = dwarf_child(die, &next, &err);
		if (rc == DW_DLV_ERROR) {
			printf("dwarf_child error: %d %s\n", rc, dwarf_errmsg(err));
			exit(1);
		}
		if (rc == DW_DLV_OK) {
			if (tag == DW_TAG_structure_type
				&& name && strcasecmp(name, struct_name) == 0) {
				find_fields(dbg, next, struct_name, field_names, field_count, level + 1);
				printf("Found struct %s but it did not have all members given!\nMissing:\n",
					struct_name);
				for (rc = 0; rc < field_count; rc++) {
					if (field_names[rc])
						printf("%s\n", field_names[rc]);
				}
				exit(3);
			}
			find_struct(dbg, next, struct_name, field_names, field_count, level + 1);
			dwarf_dealloc(dbg, next, DW_DLA_DIE);
		}


		rc = dwarf_siblingof(dbg, die, &next, &err);
		dwarf_dealloc(dbg, die, DW_DLA_DIE);
		if (name)
			dwarf_dealloc(dbg, name, DW_DLA_STRING);

		if (rc != DW_DLV_OK)
			break;

		die = next;
	} while (die);
}

static void find_fields(Dwarf_Debug dbg, Dwarf_Die die, const char *struct_name, const char *field_names[], int field_count, int level) {
	Dwarf_Die next;
	Dwarf_Error err;
	int rc, i, printed_count = 0;

	printf("struct %s {\n\tunion {\n", 
		struct_name);

	do {
		char *name;
		const char *tag_name;
		Dwarf_Half tag;

		rc = dwarf_diename(die, &name, &err);
		if (rc == DW_DLV_NO_ENTRY) {
			name = NULL;
		} else if (rc != DW_DLV_OK) {
			printf("dwarf_diename error: %d %s\n", rc, dwarf_errmsg(err));
			exit(1);
		}

		rc = dwarf_tag(die, &tag, &err);
		if (rc != DW_DLV_OK) {
			printf("dwarf_tag error: %d %s\n", rc, dwarf_errmsg(err));
			exit(1);
		}

		if (debug) {
			rc = dwarf_get_TAG_name(tag, &tag_name);
			if (rc != DW_DLV_OK) {
				printf("dwarf_get_TAG_name error: %d\n", rc);
				exit(1);
			}

			printf("<%d> %p <%d> %s: %s\n", level, die, tag, tag_name, name ? name : "<no name>");
		}

		if (tag == DW_TAG_member && name) {
			for (i = 0; i < field_count; i++) {
				if (!field_names[i])
					continue;
				if (strcasecmp(name, field_names[i]) == 0) {
					print_field(dbg, die, field_names[i], printed_count);
					field_names[i] = NULL;
					printed_count++;
					break;
				}
			}
			if (printed_count == field_count) {
				printf("\t};\n};\n");
				exit(0);
			}
		}

		rc = dwarf_siblingof(dbg, die, &next, &err);
		dwarf_dealloc(dbg, die, DW_DLA_DIE);
		if (name)
			dwarf_dealloc(dbg, name, DW_DLA_STRING);

		if (rc != DW_DLV_OK)
			break;

		die = next;
	} while (die);
}

static void print_field(Dwarf_Debug dbg, Dwarf_Die die, const char *field_name, int padnum) {
	Dwarf_Attribute attr;
	Dwarf_Error err;
	Dwarf_Unsigned offset = 0;
	char type_buf[1024];
	int rc;

	rc = dwarf_attr(die, DW_AT_data_member_location, &attr, &err);
	if (rc == DW_DLV_NO_ENTRY) {
		printf("Found %s but no offset, assuming 0\n", field_name);
	} else if (rc != DW_DLV_OK) {
		printf("Error getting dwarf attr offset: %s\n", dwarf_errmsg(err));
		exit(4);
	} else {
		Dwarf_Half form;
		rc = dwarf_whatform(attr, &form, &err);
		if (rc != DW_DLV_OK) {
			printf("Error getting whatform: %s\n", dwarf_errmsg(err));
			exit(5);
		}
		if (form == DW_FORM_data1 || form == DW_FORM_data2
			|| form == DW_FORM_data2 || form == DW_FORM_data4
			|| form == DW_FORM_data8 || form == DW_FORM_udata) {
			dwarf_formudata(attr, &offset, 0);
		} else if (form == DW_FORM_sdata) {
			Dwarf_Signed soffset;
			dwarf_formsdata(attr, &soffset, 0);
			if (soffset < 0) {
				 printf("unsupported negative offset\n");
				 /* FAIL */
			}
			offset = (Dwarf_Unsigned) soffset;
		} else {
			Dwarf_Locdesc **locdescs;
			Dwarf_Signed len;
			if (dwarf_loclist_n(attr, &locdescs, &len,  &err) == DW_DLV_ERROR) {
				 printf("unsupported member offset\n");
				 /* FAIL */
			}
			if (len != 1
				|| locdescs[0]->ld_cents != 1
				|| (locdescs[0]->ld_s[0]).lr_atom != DW_OP_plus_uconst) {
				 printf("unsupported location expression\n");
				 /* FAIL */
			}
			offset = (locdescs[0]->ld_s[0]).lr_number;
		}
		dwarf_dealloc(dbg, attr, DW_DLA_ATTR);
	}

	rc = dwarf_attr(die, DW_AT_type, &attr, &err);
	if (rc == DW_DLV_NO_ENTRY) {
		printf("Found %s but no type, can't assume that one out..\n", field_name);
		exit(6);
	} else if (rc != DW_DLV_OK) {
		printf("Error getting dwarf attrlist: %s\n", dwarf_errmsg(err));
		exit(6);
	} else {
		Dwarf_Die type_die;
		Dwarf_Off type_off;
		Dwarf_Half type_tag;
		char *type_name;
		int pointer = 0;

		rc = dwarf_global_formref(attr, &type_off, &err);
		if (rc != DW_DLV_OK) {
			printf("Error getting ref offset for type: %s\n", dwarf_errmsg(err));
			exit(7);
		}

		rc = dwarf_offdie_b(dbg, type_off, 1, &type_die, &err);
		if (rc != DW_DLV_OK) {
			printf("Error getting die from offset for type: %s\n", dwarf_errmsg(err));
			exit(7);
		}

		rc = dwarf_tag(type_die, &type_tag, &err);
		if (rc != DW_DLV_OK) {
			printf("dwarf_tag error: %d %s\n", rc, dwarf_errmsg(err));
			exit(7);
		}

		if (type_tag == DW_TAG_pointer_type) {
			Dwarf_Attribute pointer_attr;
			Dwarf_Off pointer_off;
			Dwarf_Die pointer_die;

			rc = dwarf_attr(type_die, DW_AT_type, &pointer_attr, &err);
			if (rc != DW_DLV_OK) {
				printf("Error getting pointer attr for type: %s\n", dwarf_errmsg(err));
				exit(7);
			}

			rc = dwarf_global_formref(pointer_attr, &pointer_off, &err);
			if (rc != DW_DLV_OK) {
				printf("Error getting pointer ref offset for type: %s\n", dwarf_errmsg(err));
				exit(7);
			}

			rc = dwarf_offdie_b(dbg, pointer_off, 1, &pointer_die, &err);
			if (rc != DW_DLV_OK) {
				printf("Error getting pointer die from offset for type: %s\n", dwarf_errmsg(err));
				exit(7);
			}

			dwarf_dealloc(dbg, attr, DW_DLA_ATTR);
			dwarf_dealloc(dbg, type_die, DW_DLA_DIE);
			attr = pointer_attr;
			type_die = pointer_die;
			pointer++;

			rc = dwarf_tag(type_die, &type_tag, &err);
			if (rc != DW_DLV_OK) {
				printf("dwarf_tag error: %d %s\n", rc, dwarf_errmsg(err));
				exit(7);
			}
		}

		rc = dwarf_diename(type_die, &type_name, &err);
		if (rc != DW_DLV_OK) {
			printf("dwarf_diename error: %d %s\n", rc, dwarf_errmsg(err));
			const char *tag_name;

			rc = dwarf_get_TAG_name(type_tag, &tag_name);
			if (rc != DW_DLV_OK) {
				printf("dwarf_get_TAG_name error: %d\n", rc);
			}

			printf("Bad tag %s (%d)?\n", tag_name, type_tag);
			exit(7);
		}

		if (type_tag == DW_TAG_structure_type) {
			snprintf(type_buf, 1024, "struct %s%s", pointer ? "*" : "",
				type_name);
		} else if (type_tag == DW_TAG_base_type || type_tag == DW_TAG_typedef) {
			snprintf(type_buf, 1024, "%s%s", pointer ? "*" : "",
				type_name);
		} else {
			const char *tag_name;

			rc = dwarf_get_TAG_name(type_tag, &tag_name);
			if (rc != DW_DLV_OK) {
				printf("dwarf_get_TAG_name error: %d\n", rc);
			}

			printf("Type tag %s (%d) is not implemented, please add it\n",
				tag_name, type_tag);
			exit(7);
		}

		dwarf_dealloc(dbg, type_name, DW_DLA_STRING);
		dwarf_dealloc(dbg, attr, DW_DLA_ATTR);
		dwarf_dealloc(dbg, type_die, DW_DLA_DIE);
	}

	printf("\t\tstruct {\n\t\t\tchar padding%i[%u]\n\t\t\t%s %s\n\t\t};\n",
		padnum,
		(unsigned int) offset,
		type_buf, field_name);
}