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