1*c2c66affSColin Finck /*
2*c2c66affSColin Finck * COPYRIGHT: See COPYING in the top level directory
3*c2c66affSColin Finck * PROJECT: ReactOS kernel
4*c2c66affSColin Finck * FILE: lib/rossym/find.c
5*c2c66affSColin Finck * PURPOSE: Find symbol info for an address
6*c2c66affSColin Finck *
7*c2c66affSColin Finck * PROGRAMMERS: Ge van Geldorp (gvg@reactos.com)
8*c2c66affSColin Finck */
9*c2c66affSColin Finck /*
10*c2c66affSColin Finck * Parts of this file based on work Copyright (c) 1990, 1993
11*c2c66affSColin Finck * The Regents of the University of California. All rights reserved.
12*c2c66affSColin Finck *
13*c2c66affSColin Finck * Redistribution and use in source and binary forms, with or without
14*c2c66affSColin Finck * modification, are permitted provided that the following conditions
15*c2c66affSColin Finck * are met:
16*c2c66affSColin Finck * 1. Redistributions of source code must retain the above copyright
17*c2c66affSColin Finck * notice, this list of conditions and the following disclaimer.
18*c2c66affSColin Finck * 2. Redistributions in binary form must reproduce the above copyright
19*c2c66affSColin Finck * notice, this list of conditions and the following disclaimer in the
20*c2c66affSColin Finck * documentation and/or other materials provided with the distribution.
21*c2c66affSColin Finck * 4. Neither the name of the University nor the names of its contributors
22*c2c66affSColin Finck * may be used to endorse or promote products derived from this software
23*c2c66affSColin Finck * without specific prior written permission.
24*c2c66affSColin Finck *
25*c2c66affSColin Finck * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26*c2c66affSColin Finck * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27*c2c66affSColin Finck * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28*c2c66affSColin Finck * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29*c2c66affSColin Finck * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30*c2c66affSColin Finck * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31*c2c66affSColin Finck * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32*c2c66affSColin Finck * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33*c2c66affSColin Finck * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34*c2c66affSColin Finck * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35*c2c66affSColin Finck * SUCH DAMAGE.
36*c2c66affSColin Finck */
37*c2c66affSColin Finck
38*c2c66affSColin Finck #include <precomp.h>
39*c2c66affSColin Finck
40*c2c66affSColin Finck #define NDEBUG
41*c2c66affSColin Finck #include <debug.h>
42*c2c66affSColin Finck
43*c2c66affSColin Finck BOOLEAN
RosSymGetAddressInformation(PROSSYM_INFO RosSymInfo,ULONG_PTR RelativeAddress,PROSSYM_LINEINFO RosSymLineInfo)44*c2c66affSColin Finck RosSymGetAddressInformation
45*c2c66affSColin Finck (PROSSYM_INFO RosSymInfo,
46*c2c66affSColin Finck ULONG_PTR RelativeAddress,
47*c2c66affSColin Finck PROSSYM_LINEINFO RosSymLineInfo)
48*c2c66affSColin Finck {
49*c2c66affSColin Finck ROSSYM_REGISTERS registers;
50*c2c66affSColin Finck DwarfParam params[sizeof(RosSymLineInfo->Parameters)/sizeof(RosSymLineInfo->Parameters[0])];
51*c2c66affSColin Finck DwarfSym proc = { };
52*c2c66affSColin Finck int i;
53*c2c66affSColin Finck int res = dwarfpctoline
54*c2c66affSColin Finck (RosSymInfo,
55*c2c66affSColin Finck &proc,
56*c2c66affSColin Finck RelativeAddress + RosSymInfo->pe->imagebase,
57*c2c66affSColin Finck &RosSymLineInfo->FileName,
58*c2c66affSColin Finck &RosSymLineInfo->FunctionName,
59*c2c66affSColin Finck &RosSymLineInfo->LineNumber);
60*c2c66affSColin Finck if (res == -1) {
61*c2c66affSColin Finck werrstr("Could not get basic function info");
62*c2c66affSColin Finck return FALSE;
63*c2c66affSColin Finck }
64*c2c66affSColin Finck
65*c2c66affSColin Finck if (!(RosSymLineInfo->Flags & ROSSYM_LINEINFO_HAS_REGISTERS))
66*c2c66affSColin Finck return TRUE;
67*c2c66affSColin Finck
68*c2c66affSColin Finck registers = RosSymLineInfo->Registers;
69*c2c66affSColin Finck
70*c2c66affSColin Finck DwarfExpr cfa = { };
71*c2c66affSColin Finck ulong cfaLocation;
72*c2c66affSColin Finck if (dwarfregunwind
73*c2c66affSColin Finck (RosSymInfo,
74*c2c66affSColin Finck RelativeAddress + RosSymInfo->pe->imagebase,
75*c2c66affSColin Finck proc.attrs.framebase.c,
76*c2c66affSColin Finck &cfa,
77*c2c66affSColin Finck ®isters) == -1) {
78*c2c66affSColin Finck werrstr("Can't get cfa location for %s", RosSymLineInfo->FunctionName);
79*c2c66affSColin Finck return TRUE;
80*c2c66affSColin Finck }
81*c2c66affSColin Finck
82*c2c66affSColin Finck res = dwarfgetparams
83*c2c66affSColin Finck (RosSymInfo,
84*c2c66affSColin Finck &proc,
85*c2c66affSColin Finck RelativeAddress + RosSymInfo->pe->imagebase,
86*c2c66affSColin Finck sizeof(params)/sizeof(params[0]),
87*c2c66affSColin Finck params);
88*c2c66affSColin Finck
89*c2c66affSColin Finck if (res == -1) {
90*c2c66affSColin Finck werrstr("%s: could not get params at all", RosSymLineInfo->FunctionName);
91*c2c66affSColin Finck RosSymLineInfo->NumParams = 0;
92*c2c66affSColin Finck return TRUE;
93*c2c66affSColin Finck }
94*c2c66affSColin Finck
95*c2c66affSColin Finck werrstr("%s: res %d", RosSymLineInfo->FunctionName, res);
96*c2c66affSColin Finck RosSymLineInfo->NumParams = res;
97*c2c66affSColin Finck
98*c2c66affSColin Finck res = dwarfcomputecfa(RosSymInfo, &cfa, ®isters, &cfaLocation);
99*c2c66affSColin Finck if (res == -1) {
100*c2c66affSColin Finck werrstr("%s: could not get our own cfa", RosSymLineInfo->FunctionName);
101*c2c66affSColin Finck return TRUE;
102*c2c66affSColin Finck }
103*c2c66affSColin Finck
104*c2c66affSColin Finck for (i = 0; i < RosSymLineInfo->NumParams; i++) {
105*c2c66affSColin Finck werrstr("Getting arg %s, unit %x, type %x",
106*c2c66affSColin Finck params[i].name, params[i].unit, params[i].type);
107*c2c66affSColin Finck res = dwarfargvalue
108*c2c66affSColin Finck (RosSymInfo,
109*c2c66affSColin Finck &proc,
110*c2c66affSColin Finck RelativeAddress + RosSymInfo->pe->imagebase,
111*c2c66affSColin Finck cfaLocation,
112*c2c66affSColin Finck ®isters,
113*c2c66affSColin Finck ¶ms[i]);
114*c2c66affSColin Finck if (res == -1) { RosSymLineInfo->NumParams = i; return TRUE; }
115*c2c66affSColin Finck werrstr("%s: %x", params[i].name, params[i].value);
116*c2c66affSColin Finck RosSymLineInfo->Parameters[i].ValueName = malloc(strlen(params[i].name)+1);
117*c2c66affSColin Finck strcpy(RosSymLineInfo->Parameters[i].ValueName, params[i].name);
118*c2c66affSColin Finck free(params[i].name);
119*c2c66affSColin Finck RosSymLineInfo->Parameters[i].Value = params[i].value;
120*c2c66affSColin Finck }
121*c2c66affSColin Finck
122*c2c66affSColin Finck return TRUE;
123*c2c66affSColin Finck }
124*c2c66affSColin Finck
125*c2c66affSColin Finck VOID
RosSymFreeAggregate(PROSSYM_AGGREGATE Aggregate)126*c2c66affSColin Finck RosSymFreeAggregate(PROSSYM_AGGREGATE Aggregate)
127*c2c66affSColin Finck {
128*c2c66affSColin Finck int i;
129*c2c66affSColin Finck for (i = 0; i < Aggregate->NumElements; i++) {
130*c2c66affSColin Finck free(Aggregate->Elements[i].Name);
131*c2c66affSColin Finck free(Aggregate->Elements[i].Type);
132*c2c66affSColin Finck }
133*c2c66affSColin Finck free(Aggregate->Elements);
134*c2c66affSColin Finck }
135*c2c66affSColin Finck
136*c2c66affSColin Finck BOOLEAN
RosSymAggregate(PROSSYM_INFO RosSymInfo,PCHAR Type,PROSSYM_AGGREGATE Aggregate)137*c2c66affSColin Finck RosSymAggregate(PROSSYM_INFO RosSymInfo, PCHAR Type, PROSSYM_AGGREGATE Aggregate)
138*c2c66affSColin Finck {
139*c2c66affSColin Finck char *tchar;
140*c2c66affSColin Finck ulong unit, typeoff = 0;
141*c2c66affSColin Finck DwarfSym type = { };
142*c2c66affSColin Finck // Get the first unit
143*c2c66affSColin Finck if (dwarfaddrtounit(RosSymInfo, RosSymInfo->pe->codestart + RosSymInfo->pe->imagebase, &unit) == -1)
144*c2c66affSColin Finck return FALSE;
145*c2c66affSColin Finck
146*c2c66affSColin Finck if (Type[0] == '#') {
147*c2c66affSColin Finck for (tchar = Type + 1; *tchar; tchar++) {
148*c2c66affSColin Finck typeoff *= 10;
149*c2c66affSColin Finck typeoff += *tchar - '0';
150*c2c66affSColin Finck }
151*c2c66affSColin Finck if (dwarfseeksym(RosSymInfo, unit, typeoff, &type) == -1)
152*c2c66affSColin Finck return FALSE;
153*c2c66affSColin Finck } else if (dwarflookupnameinunit(RosSymInfo, unit, Type, &type) != 0 ||
154*c2c66affSColin Finck (type.attrs.tag != TagStructType && type.attrs.tag != TagUnionType))
155*c2c66affSColin Finck return FALSE;
156*c2c66affSColin Finck
157*c2c66affSColin Finck DwarfSym element = { }, inner = { };
158*c2c66affSColin Finck int count = 0;
159*c2c66affSColin Finck
160*c2c66affSColin Finck werrstr("type %s (want %s) type %x\n", type.attrs.name, Type, type.attrs.type);
161*c2c66affSColin Finck
162*c2c66affSColin Finck if (type.attrs.have.type) {
163*c2c66affSColin Finck if (dwarfseeksym(RosSymInfo, unit, type.attrs.type, &inner) == -1)
164*c2c66affSColin Finck return FALSE;
165*c2c66affSColin Finck type = inner;
166*c2c66affSColin Finck }
167*c2c66affSColin Finck
168*c2c66affSColin Finck werrstr("finding members %d\n", type.attrs.haskids);
169*c2c66affSColin Finck while (dwarfnextsymat(RosSymInfo, &type, &element) != -1) {
170*c2c66affSColin Finck if (element.attrs.have.name)
171*c2c66affSColin Finck werrstr("%x %s\n", element.attrs.tag, element.attrs.name);
172*c2c66affSColin Finck if (element.attrs.tag == TagMember) count++;
173*c2c66affSColin Finck }
174*c2c66affSColin Finck
175*c2c66affSColin Finck werrstr("%d members\n", count);
176*c2c66affSColin Finck
177*c2c66affSColin Finck if (!count) return FALSE;
178*c2c66affSColin Finck memset(&element, 0, sizeof(element));
179*c2c66affSColin Finck Aggregate->NumElements = count;
180*c2c66affSColin Finck Aggregate->Elements = malloc(sizeof(ROSSYM_AGGREGATE_MEMBER) * count);
181*c2c66affSColin Finck count = 0;
182*c2c66affSColin Finck werrstr("Enumerating %s\n", Type);
183*c2c66affSColin Finck while (dwarfnextsymat(RosSymInfo, &type, &element) != -1) {
184*c2c66affSColin Finck memset(&Aggregate->Elements[count], 0, sizeof(*Aggregate->Elements));
185*c2c66affSColin Finck if (element.attrs.tag == TagMember) {
186*c2c66affSColin Finck if (element.attrs.have.name) {
187*c2c66affSColin Finck Aggregate->Elements[count].Name = malloc(strlen(element.attrs.name) + 1);
188*c2c66affSColin Finck strcpy(Aggregate->Elements[count].Name, element.attrs.name);
189*c2c66affSColin Finck }
190*c2c66affSColin Finck Aggregate->Elements[count].TypeId = element.attrs.type;
191*c2c66affSColin Finck // Seek our range in loc
192*c2c66affSColin Finck DwarfBuf locbuf;
193*c2c66affSColin Finck DwarfBuf instream = { };
194*c2c66affSColin Finck
195*c2c66affSColin Finck locbuf.d = RosSymInfo;
196*c2c66affSColin Finck locbuf.addrsize = RosSymInfo->addrsize;
197*c2c66affSColin Finck
198*c2c66affSColin Finck if (element.attrs.have.datamemberloc) {
199*c2c66affSColin Finck instream = locbuf;
200*c2c66affSColin Finck instream.p = element.attrs.datamemberloc.b.data;
201*c2c66affSColin Finck instream.ep = element.attrs.datamemberloc.b.data + element.attrs.datamemberloc.b.len;
202*c2c66affSColin Finck werrstr("datamemberloc type %x %p:%x\n",
203*c2c66affSColin Finck element.attrs.have.datamemberloc,
204*c2c66affSColin Finck element.attrs.datamemberloc.b.data, element.attrs.datamemberloc.b.len);
205*c2c66affSColin Finck }
206*c2c66affSColin Finck
207*c2c66affSColin Finck if (dwarfgetarg(RosSymInfo, element.attrs.name, &instream, 0, NULL, &Aggregate->Elements[count].BaseOffset) == -1)
208*c2c66affSColin Finck Aggregate->Elements[count].BaseOffset = -1;
209*c2c66affSColin Finck werrstr("tag %x name %s base %x type %x\n",
210*c2c66affSColin Finck element.attrs.tag, element.attrs.name,
211*c2c66affSColin Finck Aggregate->Elements[count].BaseOffset,
212*c2c66affSColin Finck Aggregate->Elements[count].TypeId);
213*c2c66affSColin Finck count++;
214*c2c66affSColin Finck }
215*c2c66affSColin Finck }
216*c2c66affSColin Finck for (count = 0; count < Aggregate->NumElements; count++) {
217*c2c66affSColin Finck memset(&type, 0, sizeof(type));
218*c2c66affSColin Finck memset(&inner, 0, sizeof(inner));
219*c2c66affSColin Finck werrstr("seeking type %x (%s) from %s\n",
220*c2c66affSColin Finck Aggregate->Elements[count].TypeId,
221*c2c66affSColin Finck Aggregate->Elements[count].Type,
222*c2c66affSColin Finck Aggregate->Elements[count].Name);
223*c2c66affSColin Finck dwarfseeksym(RosSymInfo, unit, Aggregate->Elements[count].TypeId, &type);
224*c2c66affSColin Finck while (type.attrs.have.type && type.attrs.tag != TagPointerType) {
225*c2c66affSColin Finck if (dwarfseeksym(RosSymInfo, unit, type.attrs.type, &inner) == -1)
226*c2c66affSColin Finck return FALSE;
227*c2c66affSColin Finck type = inner;
228*c2c66affSColin Finck }
229*c2c66affSColin Finck //dwarfdumpsym(RosSymInfo, &type);
230*c2c66affSColin Finck if (type.attrs.have.name) {
231*c2c66affSColin Finck Aggregate->Elements[count].Type = malloc(strlen(type.attrs.name) + 1);
232*c2c66affSColin Finck strcpy(Aggregate->Elements[count].Type, type.attrs.name);
233*c2c66affSColin Finck } else {
234*c2c66affSColin Finck char strbuf[128] = {'#'}, *bufptr = strbuf + 1;
235*c2c66affSColin Finck ulong idcopy = Aggregate->Elements[count].TypeId;
236*c2c66affSColin Finck ulong mult = 1;
237*c2c66affSColin Finck while (mult * 10 < idcopy) mult *= 10;
238*c2c66affSColin Finck while (mult > 0) {
239*c2c66affSColin Finck *bufptr++ = '0' + ((idcopy / mult) % 10);
240*c2c66affSColin Finck mult /= 10;
241*c2c66affSColin Finck }
242*c2c66affSColin Finck Aggregate->Elements[count].Type = malloc(strlen(strbuf) + 1);
243*c2c66affSColin Finck strcpy(Aggregate->Elements[count].Type, strbuf);
244*c2c66affSColin Finck }
245*c2c66affSColin Finck if (type.attrs.tag == TagPointerType)
246*c2c66affSColin Finck Aggregate->Elements[count].Size = RosSymInfo->addrsize;
247*c2c66affSColin Finck else
248*c2c66affSColin Finck Aggregate->Elements[count].Size = type.attrs.bytesize;
249*c2c66affSColin Finck if (type.attrs.have.bitsize)
250*c2c66affSColin Finck Aggregate->Elements[count].Bits = type.attrs.bitsize;
251*c2c66affSColin Finck if (type.attrs.have.bitoffset)
252*c2c66affSColin Finck Aggregate->Elements[count].FirstBit = type.attrs.bitoffset;
253*c2c66affSColin Finck }
254*c2c66affSColin Finck return TRUE;
255*c2c66affSColin Finck }
256*c2c66affSColin Finck
257*c2c66affSColin Finck /* EOF */
258