xref: /reactos/sdk/lib/rossym/dwarfaranges.c (revision 1734f297)
1 /*
2  * Dwarf address ranges parsing code.
3  */
4 
5 #define NTOSAPI
6 #include <ntddk.h>
7 #include <reactos/rossym.h>
8 #include "rossympriv.h"
9 #include <ntimage.h>
10 
11 #define NDEBUG
12 #include <debug.h>
13 
14 #include "dwarf.h"
15 
16 int
17 dwarfaddrtounit(Dwarf *d, ulong addr, ulong *unit)
18 {
19 	DwarfBuf b;
20 	int segsize, i;
21 	ulong len, id, off, base, size;
22 	uchar *start, *end;
23 
24 	memset(&b, 0, sizeof b);
25 	b.d = d;
26 	b.p = d->aranges.data;
27 	b.ep = b.p + d->aranges.len;
28 
29 	while(b.p < b.ep){
30 		start = b.p;
31 		len = dwarfget4(&b);
32 		if (!len) { b.ep = b.p - 4; return -1; }
33 		if((id = dwarfget2(&b)) != 2){
34 			if(b.p == nil){
35 			underflow:
36 				werrstr("buffer underflow reading address ranges header");
37 			}else
38 				werrstr("bad dwarf version 0x%x in address ranges header", id);
39 			return -1;
40 		}
41 		off = dwarfget4(&b);
42 		b.addrsize = dwarfget1(&b);
43 		if(d->addrsize == 0)
44 			d->addrsize = b.addrsize;
45 		segsize = dwarfget1(&b);
46 		USED(segsize);	/* what am i supposed to do with this? */
47 		if(b.p == nil)
48 			goto underflow;
49 		if((i = (b.p-start) % (2*b.addrsize)) != 0)
50 			b.p += 2*b.addrsize - i;
51 		end = start+4+len;
52 		while(b.p!=nil && b.p<end){
53 			base = dwarfgetaddr(&b);
54 			size = dwarfgetaddr(&b);
55 			if (!size) continue;
56 			if(b.p == nil)
57 				goto underflow;
58 			if(base <= addr && addr < base+size){
59 				*unit = off;
60 				return 0;
61 			}
62 		}
63 		if(b.p == nil)
64 			goto underflow;
65 		b.p = end;
66 	}
67 	werrstr("address 0x%lux is not listed in dwarf debugging symbols", addr);
68 	return -1;
69 }
70 
71 
72