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