summaryrefslogtreecommitdiffstats
path: root/dwarf.c
blob: b116cf52985feb665218ab456d0fa7ac524cc57f (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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include "dwarf.h"
#include "libdwarf.h"


static void parse_dwarf(Dwarf_Debug dbg, const char *struct_name, const char *field_name);
static void find_struct(Dwarf_Debug dbg, Dwarf_Die die, const char *struct_name, const char *field_name, int level);
static void find_field(Dwarf_Debug dbg, Dwarf_Die die, const char *field_name, int level);
static void print_field(Dwarf_Debug dbg, Dwarf_Die die, const char *field_name);

int debug = 0;

void usage(char *argv[]) {
	printf("%s file struct 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_name;
	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_name = 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_name);

	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_name) {
	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, next;

		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_name, 0);
	}

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

static void find_struct(Dwarf_Debug dbg, Dwarf_Die die, const char *struct_name, const char *field_name, 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_field(dbg, next, field_name, level + 1);
				printf("Found struct %s but it had no member %s!\n",
					struct_name, field_name);
				exit(3);
			}
			find_struct(dbg, next, struct_name, field_name, 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_field(Dwarf_Debug dbg, Dwarf_Die die, const char *field_name, int level) {
	Dwarf_Die next;
	Dwarf_Error err;
	int rc;

	if (level > 2)
		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>");
		}

		if (tag == DW_TAG_member && name
			&& strcasecmp(name, field_name) == 0) {
			print_field(dbg, die, field_name);
			exit(0);
		}

		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) {
			find_field(dbg, next, field_name, 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 print_field(Dwarf_Debug dbg, Dwarf_Die die, const char *field_name) {
	Dwarf_Attribute *attrs;
	Dwarf_Signed attrcount = 0;
	Dwarf_Error err;
	Dwarf_Unsigned offset = 0;
	int rc, i;

	printf("Found %s\n", field_name);

	rc = dwarf_attrlist(die, &attrs, &attrcount, &err);
	if (rc == DW_DLV_NO_ENTRY) {
		printf("Found %s but no attribute\n", field_name);
		exit(4);
	}
	if (rc != DW_DLV_OK) {
		printf("Error getting dwarf attrlist: %s\n", dwarf_errmsg(err));
		exit(4);
	}

	for (i = 0; i < attrcount; i++) {
		Dwarf_Half aform;
		rc = dwarf_whatattr(attrs[i], &aform, &err);
		if (rc != DW_DLV_OK) {
			printf("Error getting attr %d: %s\n", i, dwarf_errmsg(err));
			exit(5);
		}
		if (aform == DW_AT_data_member_location) {
			Dwarf_Half form;
			rc = dwarf_whatform(attrs[i], &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(attrs[i], &offset, 0);
			} else if (form == DW_FORM_sdata) {
				Dwarf_Signed soffset;
				dwarf_formsdata(attrs[i], &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(attrs[i], &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;
			}
		} else if (aform == DW_AT_type) {
		}
	}
	printf("offset %u\n", offset);
}